summaryrefslogtreecommitdiff
path: root/2024_rust/src/bin/day14.rs
blob: 086ec0405baa36af8bfd97b1b5e544a25b5161b7 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use regex::Regex;
use std::collections::HashSet;

fn cycle_add(u: u32, i: i32, limit: u32) -> u32 {
    let sum: i32 = u as i32 + i;
    let mut sum_u: u32 = if sum < 0 { sum + limit as i32 } else { sum } as u32;
    while sum_u >= limit {
        sum_u -= limit;
    }
    sum_u
}

#[derive(Debug)]
struct Robot {
    p: (u32, u32),
    v: (i32, i32),
}

impl Robot {
    fn parse(input: &str) -> Vec<Self> {
        let mut result: Vec<_> = vec![];
        let re = Regex::new(r"p=([0-9]+),([0-9]+) v=([0-9-]+),([0-9-]+)").unwrap();
        for (_, [px, py, vx, vy]) in re.captures_iter(input).map(|c| c.extract()) {
            result.push(Robot {
                p: (px.parse().unwrap(), py.parse().unwrap()),
                v: (vx.parse().unwrap(), vy.parse().unwrap()),
            });
        }
        // println!("Result: {result:?}");
        result
    }

    fn step(&mut self, (lx, ly): (u32, u32)) {
        let p: &mut (u32, u32) = &mut self.p;
        let v = self.v;
        p.0 = cycle_add(p.0, v.0, lx);
        p.1 = cycle_add(p.1, v.1, ly);
    }
}

struct Bathroom {
    robots: Vec<Robot>,
    limit: (u32, u32),
}

impl Bathroom {
    fn step(&mut self) {
        for r in self.robots.iter_mut() {
            r.step(self.limit);
        }
    }

    fn stepn(&mut self, seconds: u32) {
        for _i in 0..seconds {
            self.step();
        }
    }

    fn quadrant(&self, Robot { p: (px, py), .. }: &Robot) -> Option<usize> {
        let (lx, ly) = self.limit;
        let (hx, hy) = (lx / 2, ly / 2);
        if *px == hx || *py == hy {
            None
        } else {
            Some(if *px < hx { 0 } else { 1 } + if *py < hy { 0 } else { 2 })
        }
    }

    fn safety(&self) -> u32 {
        let mut scores = [0; 4];
        for r in self.robots.iter() {
            if let Some(q) = self.quadrant(r) {
                scores[q] += 1;
            }
        }
        scores.iter().product()
    }

    fn show(&self) {
        let positions: HashSet<(u32, u32)> = self.robots.iter().map(|r| r.p).collect();
        for i in 0..self.limit.1 {
            for j in 0..self.limit.0 {
                print!(
                    "{}",
                    if positions.contains(&(i, j)) {
                        "X"
                    } else {
                        "."
                    }
                );
            }
            println!();
        }
    }
}

fn p1(input: &str) -> String {
    let robots: Vec<Robot> = Robot::parse(input);
    let mut bathroom: Bathroom = Bathroom {
        robots,
        limit: (101, 103),
    };
    bathroom.stepn(100);
    dbg!(&bathroom.robots);

    let result = bathroom.safety();
    result.to_string()
}

// use std::thread::sleep;
// use std::time::Duration;

fn p2(input: &str) -> String {
    let robots: Vec<Robot> = Robot::parse(input);
    let mut bathroom: Bathroom = Bathroom {
        robots,
        limit: (101, 103),
    };
    bathroom.show();
    for i in 1.. {
        println!("\n\nAfter {} seconds:", i);
        bathroom.step();
        bathroom.show();
        // sleep(Duration::from_millis(200));
    }
    unreachable!();
}

fn main() {
    aoc2024::run_day("14", p1, p2);
}