use std::collections::HashMap; use std::fs; struct Node { left: String, right: String, } fn main() { // let file_contents = fs::read_to_string("sample.txt").expect("No input found"); let file_contents = fs::read_to_string("input.txt").expect("No input found"); let mut lines = file_contents.lines(); let steps = lines.next().expect("No steps found"); let map = lines .filter(|l| !l.is_empty()) .map(|l| { let node = &l[0..3]; let left = &l[7..10]; let right = &l[12..15]; ( node, Node { left: left.to_string(), right: right.to_string(), }, ) }) .collect::>(); let mut current = map .keys() .filter(|node| node.ends_with("A")) .map(|node| node.clone()) .collect::>(); for node in ¤t { println!("Starting node {}", node); } let mut num_steps = 0; for step in steps.chars().cycle() { if current.iter().all(|node| node.ends_with("Z")) { break; } num_steps += 1; for i in 0..current.len() { if step == 'L' { current[i] = &map[current[i]].left; } if step == 'R' { current[i] = &map[current[i]].right; } } } println!("Part 2 {}", num_steps) }