From ce111e1a627991f0fd5322f660367ab08bf167be Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 8 Dec 2023 08:54:41 +0100 Subject: [PATCH] make part 2 shorter --- 2023/src/bin/day08.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/2023/src/bin/day08.rs b/2023/src/bin/day08.rs index 8a0db2c..2284286 100644 --- a/2023/src/bin/day08.rs +++ b/2023/src/bin/day08.rs @@ -27,10 +27,6 @@ fn parse_input(raw: &str) -> Parsed { (directions, map) } -fn part1(parsed: &Parsed) -> usize { - steps_until(parsed, "AAA", "ZZZ") -} - fn steps_until((directions, map): &Parsed, start: &str, target: &str) -> usize { directions .iter() @@ -40,15 +36,16 @@ fn steps_until((directions, map): &Parsed, start: &str, target: &str) -> usize { Direction::Left => map.get(pos)?.0, Direction::Right => map.get(pos)?.1, }; - (!next.ends_with(target)).then(|| { - *pos = next; - Some(next) - }) + (!next.ends_with(target)).then(|| *pos = next) }) .count() + 1 } +fn part1(parsed: &Parsed) -> usize { + steps_until(parsed, "AAA", "ZZZ") +} + // I’m honestly not sure why this works. It seems each path only has a single ghost node, and that // node occurs right before looping, so we can just compute the least common multiple of their step counts. // I assume this holds true for other inputs (it can’t have been random),