make part 2 shorter

This commit is contained in:
kageru 2023-12-08 08:54:41 +01:00
parent bf6619d638
commit ce111e1a62

View File

@ -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),