aboutsummaryrefslogtreecommitdiff
path: root/src/day8pt1.rs
blob: ece5fd9ecd4933af5a7608f02393a72305a8777a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use regex::Regex;

pub fn main() {
    let txt = std::fs::read_to_string("./input/day8.txt").unwrap();
    let input = txt.lines().collect::<Vec<_>>();

    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::<Vec<_>>()
        })
        .collect::<Vec<_>>();

    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);
}