summaryrefslogtreecommitdiffstats
path: root/src/2021/22/mod.rs
blob: 91f56f4448e4d1bd0ede0cec76161934d335716b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
struct Point3D {
    x: i64,
    y: i64,
    z: i64,
}

impl Point3D {
    fn new(x: i64, y: i64, z: i64) -> Self {
        Self { x, y, z }
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct Range3D {
    x: std::ops::RangeInclusive<i64>,
    y: std::ops::RangeInclusive<i64>,
    z: std::ops::RangeInclusive<i64>,
}

impl Range3D {
    fn new(
        x: std::ops::RangeInclusive<i64>,
        y: std::ops::RangeInclusive<i64>,
        z: std::ops::RangeInclusive<i64>,
    ) -> Self {
        Self { x, y, z }
    }

    fn contains(&self, point: &Point3D) -> bool {
        self.x.contains(&point.x)
            && self.y.contains(&point.y)
            && self.z.contains(&point.z)
    }
}

#[derive(Debug, Clone)]
struct Rule {
    on: bool,
    range: Range3D,
}

impl Rule {
    fn parse(line: &str) -> Self {
        let captures = regex_captures!(
            r"^(\w+) x=(-?\d+)..(-?\d+),y=(-?\d+)..(-?\d+),z=(-?\d+)..(-?\d+)$",
            line
        )
        .unwrap();
        Self {
            on: &captures[1] == "on",
            range: Range3D::new(
                captures[2].parse().unwrap()..=captures[3].parse().unwrap(),
                captures[4].parse().unwrap()..=captures[5].parse().unwrap(),
                captures[6].parse().unwrap()..=captures[7].parse().unwrap(),
            ),
        }
    }

    fn contains(&self, point: &Point3D) -> Option<bool> {
        if self.range.contains(point) {
            Some(self.on)
        } else {
            None
        }
    }
}

#[derive(Debug)]
pub struct Reactor {
    rules: Vec<Rule>,
}

impl Reactor {
    fn parse(lines: impl Iterator<Item = String>) -> Self {
        Self {
            rules: lines.map(|line| Rule::parse(&line)).collect(),
        }
    }

    fn on(&self, point: &Point3D) -> bool {
        for rule in self.rules.iter().rev() {
            if let Some(on) = rule.contains(point) {
                return on;
            }
        }
        false
    }
}

pub fn parse(fh: File) -> Result<Reactor> {
    Ok(Reactor::parse(parse::lines(fh)))
}

pub fn part1(reactor: Reactor) -> Result<i64> {
    let mut total = 0;
    for x in -50..=50 {
        for y in -50..=50 {
            for z in -50..=50 {
                if reactor.on(&Point3D::new(x, y, z)) {
                    total += 1;
                }
            }
        }
    }
    Ok(total)
}

pub fn part2(reactor: Reactor) -> Result<i64> {
    let mut x = vec![];
    let mut y = vec![];
    let mut z = vec![];
    for rule in &reactor.rules {
        x.push(*rule.range.x.start());
        x.push(rule.range.x.end() + 1);
        y.push(*rule.range.y.start());
        y.push(rule.range.y.end() + 1);
        z.push(*rule.range.z.start());
        z.push(rule.range.z.end() + 1);
    }
    x.sort_unstable();
    y.sort_unstable();
    z.sort_unstable();

    let mut total = 0;
    for i in 0..(x.len() - 1) {
        eprintln!("{}", i);
        for j in 0..(y.len() - 1) {
            for k in 0..(z.len() - 1) {
                if reactor.on(&Point3D::new(x[i], y[j], z[k])) {
                    total += (x[i + 1] - x[i])
                        * (y[j + 1] - y[j])
                        * (z[k + 1] - z[k])
                }
            }
        }
    }
    Ok(total)
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(parse::data(2021, 22).unwrap()).unwrap()).unwrap(),
        570915
    );
    assert_eq!(
        part2(parse(parse::data(2021, 22).unwrap()).unwrap()).unwrap(),
        1268313839428137
    );
}