make part 2 shorter
This commit is contained in:
parent
bf6619d638
commit
ce111e1a62
|
@ -27,10 +27,6 @@ fn parse_input(raw: &str) -> Parsed {
|
||||||
(directions, map)
|
(directions, map)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(parsed: &Parsed) -> usize {
|
|
||||||
steps_until(parsed, "AAA", "ZZZ")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn steps_until((directions, map): &Parsed, start: &str, target: &str) -> usize {
|
fn steps_until((directions, map): &Parsed, start: &str, target: &str) -> usize {
|
||||||
directions
|
directions
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -40,15 +36,16 @@ fn steps_until((directions, map): &Parsed, start: &str, target: &str) -> usize {
|
||||||
Direction::Left => map.get(pos)?.0,
|
Direction::Left => map.get(pos)?.0,
|
||||||
Direction::Right => map.get(pos)?.1,
|
Direction::Right => map.get(pos)?.1,
|
||||||
};
|
};
|
||||||
(!next.ends_with(target)).then(|| {
|
(!next.ends_with(target)).then(|| *pos = next)
|
||||||
*pos = next;
|
|
||||||
Some(next)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.count()
|
.count()
|
||||||
+ 1
|
+ 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
|
// 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.
|
// 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),
|
// I assume this holds true for other inputs (it can’t have been random),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user