TwoShedsDerrick

Untitled

Dec 8th, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.02 KB | None | 0 0
  1. pub fn part_two(input: &str) -> Option<usize> {
  2.     let mut lines = input.lines();
  3.     let steps: Vec<char> = lines.next()?.chars().collect();
  4.  
  5.     lines.next()?;
  6.  
  7.     type Node = (char, char, char);
  8.     let moves: HashMap<Node, (Node, Node)> = lines
  9.         .map(|line| {
  10.             let l: Vec<char> = line.chars().collect();
  11.             ((l[0], l[1], l[2]), ((l[7], l[8], l[9]), (l[12], l[13], l[14])))
  12.         })
  13.         .collect();
  14.  
  15.     Some(
  16.         moves
  17.             .keys()
  18.             .filter(|pos| pos.2 == 'A')
  19.             .map(|pos: &Node| {
  20.                 steps
  21.                     .iter()
  22.                     .cycle()
  23.                     .scan(pos, |p, &dir: &char|
  24.                         match p.2 {
  25.                             'Z' => None,
  26.                             _ => Some(*p = match dir { 'L' => &moves[p].0, _ => &moves[p].1})
  27.                         }
  28.                     )
  29.                     .count()
  30.             })
  31.             .fold(1, |a, b| a * b / gcd::binary_usize(a, b)),
  32.     )
  33. }
Add Comment
Please, Sign In to add comment