Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn part_two(input: &str) -> Option<usize> {
- let mut lines = input.lines();
- let steps: Vec<char> = lines.next()?.chars().collect();
- lines.next()?;
- type Node = (char, char, char);
- let moves: HashMap<Node, (Node, Node)> = lines
- .map(|line| {
- let l: Vec<char> = line.chars().collect();
- ((l[0], l[1], l[2]), ((l[7], l[8], l[9]), (l[12], l[13], l[14])))
- })
- .collect();
- Some(
- moves
- .keys()
- .filter(|pos| pos.2 == 'A')
- .map(|pos: &Node| {
- steps
- .iter()
- .cycle()
- .scan(pos, |p, &dir: &char|
- match p.2 {
- 'Z' => None,
- _ => Some(*p = match dir { 'L' => &moves[p].0, _ => &moves[p].1})
- }
- )
- .count()
- })
- .fold(1, |a, b| a * b / gcd::binary_usize(a, b)),
- )
- }
Add Comment
Please, Sign In to add comment