From f94c6c48cc7c1c691c22b6ecab90d5a86b11d525 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 7 Dec 2020 22:32:11 +0000 Subject: [PATCH] day7 done i guess --- Makefile | 3 +- day4/Makefile | 6 ++-- {fuck7 => day7}/Cargo.toml | 2 ++ day7/Makefile | 24 ++++++++++++++ {fuck7 => day7}/src/bag.rs | 6 +++- day7/src/main.rs | 53 +++++++++++++++++++++++++++++ {fuck7 => day7}/src/parse.rs | 0 fuck7/src/main.rs | 64 ------------------------------------ 8 files changed, 90 insertions(+), 68 deletions(-) rename {fuck7 => day7}/Cargo.toml (95%) create mode 100644 day7/Makefile rename {fuck7 => day7}/src/bag.rs (93%) create mode 100644 day7/src/main.rs rename {fuck7 => day7}/src/parse.rs (100%) delete mode 100644 fuck7/src/main.rs diff --git a/Makefile b/Makefile index a8b77e7..101e94a 100644 --- a/Makefile +++ b/Makefile @@ -11,13 +11,14 @@ INCLUDE=$(shell pwd)/common/include COMMON_FLAGS=-pipe -O3 -Wall -Wextra -Wstrict-aliasing -pedantic $(COMMON_OPT_FLAGS) +RUSTFLAGS+=-C target-cpu=native CFLAGS+=$(COMMON_FLAGS) --std=gnu11 $(C_OPT_FLAGS) CXXFLAGS+=$(COMMON_FLAGS) --std=gnu++20 $(CXX_OPT_FLAGS) LDFLAGS+=$(LD_OPT_FLAGS) DAYS= $(wildcard day*) -ENV= CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" INCLUDE="$(INCLUDE)" +ENV= CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" INCLUDE="$(INCLUDE)" RUSTFLAGS="$(RUSTFLAGS)" .PHONY: all diff --git a/day4/Makefile b/day4/Makefile index 9c6ed05..a450d56 100644 --- a/day4/Makefile +++ b/day4/Makefile @@ -1,6 +1,8 @@ CARGO_FEATURE_FLAGS?= +RUSTFLAGS?=-C target-cpu=native + .PHONY: all .NOTPARALLEL: all all: @@ -9,13 +11,13 @@ all: .NOTPARALLEL: part1 part1: $(wildcard src/*.rs) - cargo build --release $(addprefix --features ,$(CARGO_FEATURE_FLAGS)) + RUSTFLAGS="$(RUSTFLAGS)" cargo build --release $(addprefix --features ,$(CARGO_FEATURE_FLAGS)) mv -f target/release/day4 $@ strip $@ .NOTPARALLEL: part2 part2: $(wildcard src/*.rs) - cargo build --release --features $@ $(addprefix --features ,$(CARGO_FEATURE_FLAGS)) + RUSTFLAGS="$(RUSTFLAGS)" cargo build --release --features $@ $(addprefix --features ,$(CARGO_FEATURE_FLAGS)) mv -f target/release/day4 $@ strip $@ diff --git a/fuck7/Cargo.toml b/day7/Cargo.toml similarity index 95% rename from fuck7/Cargo.toml rename to day7/Cargo.toml index 1cac87e..d3e555e 100644 --- a/fuck7/Cargo.toml +++ b/day7/Cargo.toml @@ -7,6 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] + +part2 = [] test = [] [dependencies] diff --git a/day7/Makefile b/day7/Makefile new file mode 100644 index 0000000..534ae70 --- /dev/null +++ b/day7/Makefile @@ -0,0 +1,24 @@ +CARGO_FEATURE_FLAGS?= + +RUSTFLAGS?=-C target-cpu=native + +.PHONY: all +.NOTPARALLEL: all +all: + $(MAKE) part1 + $(MAKE) part2 + +.NOTPARALLEL: part1 +part1: $(wildcard src/*.rs) + RUSTFLAGS="$(RUSTFLAGS)" cargo build --release $(addprefix --features ,$(CARGO_FEATURE_FLAGS)) + mv -f target/release/day7 $@ + strip $@ + +.NOTPARALLEL: part2 +part2: $(wildcard src/*.rs) + RUSTFLAGS="$(RUSTFLAGS)" cargo build --release --features $@ $(addprefix --features ,$(CARGO_FEATURE_FLAGS)) + mv -f target/release/day7 $@ + strip $@ + +clean: + rm -f part{1,2} diff --git a/fuck7/src/bag.rs b/day7/src/bag.rs similarity index 93% rename from fuck7/src/bag.rs rename to day7/src/bag.rs index 8363791..aeff913 100644 --- a/fuck7/src/bag.rs +++ b/day7/src/bag.rs @@ -35,6 +35,10 @@ impl Hash for Rule { impl Rule { + pub fn children(&self) -> &[(usize, BagRef)] + { + self.contains.as_slice() + } #[inline] pub fn name(&self) -> &str { &self.bag[..] @@ -43,7 +47,7 @@ impl Rule /// Find the rules for each inner bag within this context pub fn inner_rules<'a>(&'a self, hashes: &'a Bags) -> impl Iterator + 'a { - self.contains.iter().filter_map(move |(n, re)| Some(std::iter::repeat(hashes.get(re)?).take(*n))).flatten() + self.contains.iter().filter_map(move |(_, re)| hashes.get(re)) } #[inline] pub fn new(bag: impl Into, contains: impl IntoIterator) -> Self { diff --git a/day7/src/main.rs b/day7/src/main.rs new file mode 100644 index 0000000..5fed593 --- /dev/null +++ b/day7/src/main.rs @@ -0,0 +1,53 @@ +#![feature(str_split_once)] + +#![allow(dead_code)] + +use std::{ + io::BufReader, + fs::OpenOptions, + collections::HashSet, +}; + +#[cfg(feature="test")] +const INPUT: &str ="input-test"; +#[cfg(not(feature="test"))] +const INPUT: &str = "input"; + +const NEEDLE: &str = "shiny gold"; + +mod parse; +mod bag; + +fn cnt_rules(rule: &bag::Rule, set: &HashSet) -> usize +{ + match rule.children() { + &[] => 0, + children => { + children.iter().map(|(n, b)| *n + (*n * cnt_rules(set.get(b).unwrap(), set))).sum::() + }, + } +} + +fn main() -> Result<(), Box> { + let parsed: HashSet<_> = parse::parse(BufReader::new(OpenOptions::new().read(true).open(INPUT)?)).collect(); + + #[cfg(not(feature="part2"))] + { + let mut found=0; + for rule in parsed.iter() { + #[cfg(debug_assertions)] + eprintln!("{:?}", rule); + + if rule.name() != NEEDLE { + if rule.all_rules(&parsed).search(NEEDLE).is_some() { + found +=1; + } + } + } + + println!("{}", found); + } + #[cfg(feature="part2")] println!("{}", cnt_rules(parsed.get(NEEDLE).unwrap(), &parsed)); + + Ok(()) +} diff --git a/fuck7/src/parse.rs b/day7/src/parse.rs similarity index 100% rename from fuck7/src/parse.rs rename to day7/src/parse.rs diff --git a/fuck7/src/main.rs b/fuck7/src/main.rs deleted file mode 100644 index aa7565c..0000000 --- a/fuck7/src/main.rs +++ /dev/null @@ -1,64 +0,0 @@ -#![feature(str_split_once)] - -#![allow(dead_code)] - -use std::{ - io::BufReader, - fs::OpenOptions, - collections::HashSet, -}; - -#[cfg(feature="test")] -const INPUT: &str ="input-test2"; -#[cfg(not(feature="test"))] -const INPUT: &str = "input"; - -const NEEDLE: &str = "shiny gold"; - -mod parse; -mod bag; - -fn cnt_rules(rule: &bag::Rule, set: &HashSet) -> usize -{ - let mut already_done: HashSet<&bag::Rule> = HashSet::new(); - rule.all_rules(set).map(|x| { - let mut o = cnt_rules(x, set) + x.inner_rules(set).count(); - - if already_done.insert(x) || true { - if o > 0 { - eprintln!("{:?} -> {}", x, o); - o+=1; - } else if !already_done.contains(x) { - eprintln!("{:?} -> {}", x ,1); - return 1; - } - o - } else { - 0 - } - }).sum() -} - -fn main() -> Result<(), Box> { - let parsed: HashSet<_> = parse::parse(BufReader::new(OpenOptions::new().read(true).open(INPUT)?)).collect(); - - let mut found=0; - for rule in parsed.iter() { - #[cfg(debug_assertions)] - eprintln!("{:?}", rule); - - if rule.name() != NEEDLE { - if rule.all_rules(&parsed).search(NEEDLE).is_some() { - found +=1; - } - } - } - - println!("{}", found); - - //#[cfg(feature="part2")] - { - println!("{}", cnt_rules(parsed.get(NEEDLE).unwrap(), &parsed)); - } - Ok(()) -}