summaryrefslogtreecommitdiff
path: root/2023/rust/day8/src/lib.rs
blob: 31f2e2c29ca42f2a0ba098f5a7fa23904cea3b3d (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::collections::HashMap;

pub fn part1(input: &str) -> u32 {
    let mut res: u32 = 0;

    let lines: Vec<String> = input
        .trim_end()
        .split('\n')
        .map(|x| x.to_string())
        .collect();
    let ltype = &lines[0];
    let mut hm: HashMap<String, (String, String)> = HashMap::new();
    for play in &lines[2..] {
        hm.insert(
            play[..3].to_string(),
            (play[7..10].to_string(), play[12..15].to_string()),
        );
    }

    let mut current = "AAA".to_string();
    for i in ltype.chars().cycle() {
        current = match i {
            'R' => hm.get(&current).unwrap().1.clone(),
            'L' => hm.get(&current).unwrap().0.clone(),
            _ => panic!("wtf is this?"),
        };
        res += 1;

        if current == "ZZZ".to_string() {
            break;
        }
    }

    res
}

pub fn part2(input: &str) -> u32 {
    let mut res: u32 = 0;

    let lines: Vec<String> = input
        .trim_end()
        .split('\n')
        .map(|x| x.to_string())
        .collect();
    let ltype = &lines[0];
    let mut hm: HashMap<String, (String, String)> = HashMap::new();

    for play in &lines[2..] {
        hm.insert(
            play[..3].to_string(),
            (play[7..10].to_string(), play[12..15].to_string()),
        );
    }

    let mut currents: Vec<String> = hm
        .keys()
        .filter(|x| x.ends_with("A"))
        .into_iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>();

    dbg!("{currents:?}");
    'o: for i in ltype.chars().cycle() {
        for k in 0..currents.len() {
            currents[k] = match i {
                'R' => hm.get(&currents[k]).unwrap().1.clone(),
                'L' => hm.get(&currents[k]).unwrap().0.clone(),
                _ => panic!("wtf is this?"),
            };
            res += 1;
        }

        for current in &currents {
            if !current.ends_with("Z") {
                continue 'o;
            }
        }
        break;
    }

    res
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn example_part1() {
        let input = include_str!("../example.txt");
        let result = part1(input);
        assert_eq!(result, 2);
    }

    #[test]
    fn input_part1() {
        let input = include_str!("../input.txt");
        let result = part1(input);
        assert_eq!(result, 21389);
    }

    #[test]
    fn example_part2() {
        let input = include_str!("../example2.txt");
        let result = part2(input);
        assert_eq!(result, 12);
    }

    // #[test]
    // fn input_part2() {
    //     let input = include_str!("../input.txt");
    //     let result = part2(input);
    //     assert_eq!(result, 248179786);
    // }
}