use regex::Regex; pub fn main() { let txt = std::fs::read_to_string("./input/day8.txt").unwrap(); let input = txt.lines().collect::>(); let instr = input[0]; let graf = (&input[2..input.len()]) .into_iter() .map(|s| { let re = Regex::new(r"=\s*|\(|\)|,").unwrap(); re.replace_all(s, "") .split_whitespace() .map(String::from) .collect::>() }) .collect::>(); let mut cnt: u32 = 0; let mut trn = "AAA"; while trn != "ZZZ" { for cvor in &graf { if cvor[0] == trn { if instr.chars().nth(cnt as usize % instr.len()).unwrap() == 'L' { trn = cvor[1].as_str(); } else { trn = cvor[2].as_str(); } cnt += 1; break; } } } println!("{}", cnt); }