From d98c2e778f2b6bba77ee973c10816749336b8887 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 4 Dec 2016 14:17:42 -0500 Subject: Day 4 part 2 --- src/bin/day04.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/bin/day04.rs b/src/bin/day04.rs index 9f7d0ab..c6a9709 100644 --- a/src/bin/day04.rs +++ b/src/bin/day04.rs @@ -26,7 +26,7 @@ impl FromStr for Room { } impl Room { - fn real(&self) -> bool { + fn checksum(&self) -> String { let mut letters = [ (0, 'a'), (0, 'b'), (0, 'c'), (0, 'd'), (0, 'e'), (0, 'f'), (0, 'g'), (0, 'h'), (0, 'i'), (0, 'j'), (0, 'k'), (0, 'l'), (0, 'm'), (0, 'n'), (0, 'o'), (0, 'p'), @@ -41,29 +41,71 @@ impl Room { letters.sort(); - let expected: String = letters.into_iter() + letters.into_iter() .map(|l| l.1) .take(5) - .collect(); + .collect() + } + + fn verify_checksum(&self) -> bool { + self.checksum == self.checksum() + } - expected == self.checksum + fn decrypt_name(&self) -> String { + self.name.chars() + .map(|c| { + match c { + '-' => ' ', + _ => rotate(c, self.sector_id), + } + }) + .collect() } } -fn solve(input: &str) -> u32 { +fn rotate(c: char, n: u32) -> char { + let mut c = c; + for _ in 0..n { + c = match c { + 'a' ... 'y' => (c as u8 + 1) as char, + 'z' => 'a', + _ => panic!("cannot rotate {}", c), + } + } + c +} + +fn solve1(input: &str) -> u32 { input.lines() .map(str::parse) .map(Result::unwrap) - .filter(Room::real) + .filter(Room::verify_checksum) .map(|room| room.sector_id) .sum() } +fn solve2(input: &str) -> Option { + input.lines() + .map(str::parse) + .map(Result::unwrap) + .filter(Room::verify_checksum) + .filter_map(|r| { + match r.decrypt_name().as_str() { + "northpole object storage" => Some(r.sector_id), + _ => None, + } + }) + .next() +} + fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); - println!("Part 1: {}", solve(&input)); + println!("Part 1: {}", solve1(&input)); + if let Some(sector_id) = solve2(&input) { + println!("Part 2: {}", sector_id); + } } #[test] @@ -74,5 +116,11 @@ a-b-c-d-e-f-g-h-987[abcde] not-a-real-room-404[oarel] totally-real-room-200[decoy] "; - assert_eq!(1514, solve(&input[1..])); + assert_eq!(1514, solve1(&input[1..])); +} + +#[test] +fn part2() { + let room: Room = "qzmt-zixmtkozy-ivhz-343[zimth]".parse().unwrap(); + assert_eq!("very encrypted name", room.decrypt_name()); } -- cgit 1.4.1 lass='logmsg'> 2021-01-13Fix C lexer to require a digit in a float literalJune McEnroe Otherwise ".l" is matched as Number. 2021-01-13Support long double in c.shJune McEnroe 2021-01-13Update Terminal.app coloursJune McEnroe God what is this garbage I only changed one colour. 2021-01-13Increase dark white brightness slightlyJune McEnroe 2021-01-13Add hilex example to htagml manualJune McEnroe 2021-01-12Style causal.agency like bin HTMLJune McEnroe 2021-01-12Avoid matching tag text inside HTML elementsJune McEnroe 2021-01-12Use hilex for up -hJune McEnroe 2021-01-12Use hilex for bin HTMLJune McEnroe 2021-01-12Don't output a pre in hilex by defaultJune McEnroe 2021-01-12Move hilex out of hilex directoryJune McEnroe 2021-01-12Consolidate hilex formatters into hilex.cJune McEnroe 2021-01-12Remove hacky tagging from hilexJune McEnroe God that makes the lexers so much simpler. 2021-01-12Add htagml -iJune McEnroe 2021-01-12Render tag index in HTMLJune McEnroe 2021-01-12Add htagml -xJune McEnroe 2021-01-12Prevent matching the same tag twiceJune McEnroe 2021-01-12Process htagml file line by lineJune McEnroe This simplifies some things, adds support for line number tag definitions, and should enable combining htagml with other preprocessors in the future. 2021-01-12Split fields by tab onlyJune McEnroe Also don't fail hard on non-forward-search definitions. 2021-01-12List both Makefile and html.sh under README.7June McEnroe 2021-01-12Add htagml exampleJune McEnroe 2021-01-12Use mandoc and htagml for bin htmlJune McEnroe 2021-01-12Add htagmlJune McEnroe 2021-01-12Replace causal.agency with a simple mdoc pageJune McEnroe 2021-01-11Publish "Using vi"June McEnroe 2021-01-11Enable diff.colorMovedJune McEnroe 2021-01-10Set less search case-insensitiveJune McEnroe 2021-01-10Set EXINITJune McEnroe neovim is laggy as hell in my OpenBSD VM, so I switched to vi so I could type without getting frustrated. 2021-01-09Add c -t flag to print expression typeJune McEnroe Also add missing float case. 2021-01-05Update taglineJune McEnroe