advent_of_code/year2021/
day22.rs

1use crate::input::Input;
2
3pub fn solve(input: &Input) -> Result<u64, String> {
4    let mut cuboids = input
5        .text
6        .lines()
7        .map(Cuboid::parse)
8        .collect::<Option<Vec<_>>>()
9        .ok_or("Invalid input")?;
10
11    if input.is_part_one() {
12        cuboids.retain(|c| {
13            !(c.max_corner.x < -50
14                || c.min_corner.x > 50
15                || c.max_corner.y < -50
16                || c.min_corner.y > 50
17                || c.max_corner.z < -50
18                || c.min_corner.z > 50)
19        });
20        for c in cuboids.iter_mut() {
21            c.min_corner.x = c.min_corner.x.clamp(-50, 50);
22            c.max_corner.x = c.max_corner.x.clamp(-50, 50);
23            c.min_corner.y = c.min_corner.y.clamp(-50, 50);
24            c.max_corner.y = c.max_corner.y.clamp(-50, 50);
25            c.min_corner.z = c.min_corner.z.clamp(-50, 50);
26            c.max_corner.z = c.max_corner.z.clamp(-50, 50);
27        }
28    }
29
30    Ok(Cuboid::combine_and_count_enclosed_cubes(&cuboids))
31}
32
33#[derive(Clone, Default)]
34struct Coordinate {
35    x: i32,
36    y: i32,
37    z: i32,
38}
39
40#[derive(Clone)]
41struct Cuboid {
42    on: bool,
43    min_corner: Coordinate,
44    max_corner: Coordinate,
45}
46
47impl Cuboid {
48    fn parse(text: &str) -> Option<Self> {
49        let on = text.starts_with("on");
50        let range_parts = text.split(&['=', '.', ','][..]).collect::<Vec<_>>();
51        if range_parts.len() != 12 {
52            return None;
53        }
54        let x_min = range_parts[1].parse::<i32>().ok()?;
55        let x_max = range_parts[3].parse::<i32>().ok()?;
56        let y_min = range_parts[5].parse::<i32>().ok()?;
57        let y_max = range_parts[7].parse::<i32>().ok()?;
58        let z_min = range_parts[9].parse::<i32>().ok()?;
59        let z_max = range_parts[11].parse::<i32>().ok()?;
60        Some(Self {
61            on,
62            min_corner: Coordinate {
63                x: x_min,
64                y: y_min,
65                z: z_min,
66            },
67            max_corner: Coordinate {
68                x: x_max,
69                y: y_max,
70                z: z_max,
71            },
72        })
73    }
74
75    fn intersect_with(&self, other: &Self) -> Option<Self> {
76        let x_intersection = Self::intersect_intervals(
77            (self.min_corner.x, self.max_corner.x),
78            (other.min_corner.x, other.max_corner.x),
79        )?;
80        let y_intersection = Self::intersect_intervals(
81            (self.min_corner.y, self.max_corner.y),
82            (other.min_corner.y, other.max_corner.y),
83        )?;
84        let z_intersection = Self::intersect_intervals(
85            (self.min_corner.z, self.max_corner.z),
86            (other.min_corner.z, other.max_corner.z),
87        )?;
88
89        Some(Self {
90            // Matching on (self.on, other.on):
91            // (on, on): Avoid double counting (as other whole cuboid will be added).
92            // (on, off): Turning off a turned on cuboid.
93            // (off, on): Turning on a turned off cuboid.
94            // (off, off): Turning off a turned off correction cuboid.
95            on: !self.on,
96            min_corner: Coordinate {
97                x: x_intersection.0,
98                y: y_intersection.0,
99                z: z_intersection.0,
100            },
101            max_corner: Coordinate {
102                x: x_intersection.1,
103                y: y_intersection.1,
104                z: z_intersection.1,
105            },
106        })
107    }
108
109    fn intersect_intervals(interval_a: (i32, i32), interval_b: (i32, i32)) -> Option<(i32, i32)> {
110        if interval_b.0 > interval_a.1 || interval_a.0 > interval_b.1 {
111            None
112        } else {
113            let intersection_start = std::cmp::max(interval_a.0, interval_b.0);
114            let intersection_end = std::cmp::min(interval_a.1, interval_b.1);
115            Some((intersection_start, intersection_end))
116        }
117    }
118
119    const fn enclosed_cubes(&self) -> u64 {
120        (1 + self.max_corner.x - self.min_corner.x) as u64
121            * (1 + self.max_corner.y - self.min_corner.y) as u64
122            * (1 + self.max_corner.z - self.min_corner.z) as u64
123    }
124
125    fn combine_and_count_enclosed_cubes(cuboids: &[Self]) -> u64 {
126        let mut combined = Vec::with_capacity(cuboids.len() / 2);
127        for cuboid in cuboids {
128            combined.append(
129                &mut combined
130                    .iter()
131                    .filter_map(|already_combined: &Self| already_combined.intersect_with(cuboid))
132                    .collect::<Vec<_>>(),
133            );
134            if cuboid.on {
135                combined.push(cuboid.clone());
136            }
137        }
138
139        combined
140            .iter()
141            .map(|cuboid| {
142                let multiplier = if cuboid.on { 1 } else { -1 };
143                multiplier * (cuboid.enclosed_cubes() as i64)
144            })
145            .sum::<i64>() as u64
146    }
147}
148
149#[test]
150pub fn tests() {
151    use crate::input::{test_part_one, test_part_two};
152
153    let example = "on x=10..12,y=10..12,z=10..12
154on x=11..13,y=11..13,z=11..13
155off x=9..11,y=9..11,z=9..11
156on x=10..10,y=10..10,z=10..10";
157    test_part_one!(example => 39);
158    let example = "on x=-20..26,y=-36..17,z=-47..7
159on x=-20..33,y=-21..23,z=-26..28
160on x=-22..28,y=-29..23,z=-38..16
161on x=-46..7,y=-6..46,z=-50..-1
162on x=-49..1,y=-3..46,z=-24..28
163on x=2..47,y=-22..22,z=-23..27
164on x=-27..23,y=-28..26,z=-21..29
165on x=-39..5,y=-6..47,z=-3..44
166on x=-30..21,y=-8..43,z=-13..34
167on x=-22..26,y=-27..20,z=-29..19
168off x=-48..-32,y=26..41,z=-47..-37
169on x=-12..35,y=6..50,z=-50..-2
170off x=-48..-32,y=-32..-16,z=-15..-5
171on x=-18..26,y=-33..15,z=-7..46
172off x=-40..-22,y=-38..-28,z=23..41
173on x=-16..35,y=-41..10,z=-47..6
174off x=-32..-23,y=11..30,z=-14..3
175on x=-49..-5,y=-3..45,z=-29..18
176off x=18..30,y=-20..-8,z=-3..13
177on x=-41..9,y=-7..43,z=-33..15
178on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
179on x=967..23432,y=45373..81175,z=27513..53682";
180    test_part_one!(example => 590_784);
181    let example = "on x=-5..47,y=-31..22,z=-19..33
182on x=-44..5,y=-27..21,z=-14..35
183on x=-49..-1,y=-11..42,z=-10..38
184on x=-20..34,y=-40..6,z=-44..1
185off x=26..39,y=40..50,z=-2..11
186on x=-41..5,y=-41..6,z=-36..8
187off x=-43..-33,y=-45..-28,z=7..25
188on x=-33..15,y=-32..19,z=-34..11
189off x=35..47,y=-46..-34,z=-11..5
190on x=-14..36,y=-6..44,z=-16..29
191on x=-57795..-6158,y=29564..72030,z=20435..90618
192on x=36731..105352,y=-21140..28532,z=16094..90401
193on x=30999..107136,y=-53464..15513,z=8553..71215
194on x=13528..83982,y=-99403..-27377,z=-24141..23996
195on x=-72682..-12347,y=18159..111354,z=7391..80950
196on x=-1060..80757,y=-65301..-20884,z=-103788..-16709
197on x=-83015..-9461,y=-72160..-8347,z=-81239..-26856
198on x=-52752..22273,y=-49450..9096,z=54442..119054
199on x=-29982..40483,y=-108474..-28371,z=-24328..38471
200on x=-4958..62750,y=40422..118853,z=-7672..65583
201on x=55694..108686,y=-43367..46958,z=-26781..48729
202on x=-98497..-18186,y=-63569..3412,z=1232..88485
203on x=-726..56291,y=-62629..13224,z=18033..85226
204on x=-110886..-34664,y=-81338..-8658,z=8914..63723
205on x=-55829..24974,y=-16897..54165,z=-121762..-28058
206on x=-65152..-11147,y=22489..91432,z=-58782..1780
207on x=-120100..-32970,y=-46592..27473,z=-11695..61039
208on x=-18631..37533,y=-124565..-50804,z=-35667..28308
209on x=-57817..18248,y=49321..117703,z=5745..55881
210on x=14781..98692,y=-1341..70827,z=15753..70151
211on x=-34419..55919,y=-19626..40991,z=39015..114138
212on x=-60785..11593,y=-56135..2999,z=-95368..-26915
213on x=-32178..58085,y=17647..101866,z=-91405..-8878
214on x=-53655..12091,y=50097..105568,z=-75335..-4862
215on x=-111166..-40997,y=-71714..2688,z=5609..50954
216on x=-16602..70118,y=-98693..-44401,z=5197..76897
217on x=16383..101554,y=4615..83635,z=-44907..18747
218off x=-95822..-15171,y=-19987..48940,z=10804..104439
219on x=-89813..-14614,y=16069..88491,z=-3297..45228
220on x=41075..99376,y=-20427..49978,z=-52012..13762
221on x=-21330..50085,y=-17944..62733,z=-112280..-30197
222on x=-16478..35915,y=36008..118594,z=-7885..47086
223off x=-98156..-27851,y=-49952..43171,z=-99005..-8456
224off x=2032..69770,y=-71013..4824,z=7471..94418
225on x=43670..120875,y=-42068..12382,z=-24787..38892
226off x=37514..111226,y=-45862..25743,z=-16714..54663
227off x=25699..97951,y=-30668..59918,z=-15349..69697
228off x=-44271..17935,y=-9516..60759,z=49131..112598
229on x=-61695..-5813,y=40978..94975,z=8655..80240
230off x=-101086..-9439,y=-7088..67543,z=33935..83858
231off x=18020..114017,y=-48931..32606,z=21474..89843
232off x=-77139..10506,y=-89994..-18797,z=-80..59318
233off x=8476..79288,y=-75520..11602,z=-96624..-24783
234on x=-47488..-1262,y=24338..100707,z=16292..72967
235off x=-84341..13987,y=2429..92914,z=-90671..-1318
236off x=-37810..49457,y=-71013..-7894,z=-105357..-13188
237off x=-27365..46395,y=31009..98017,z=15428..76570
238off x=-70369..-16548,y=22648..78696,z=-1892..86821
239on x=-53470..21291,y=-120233..-33476,z=-44150..38147
240off x=-93533..-4276,y=-16170..68771,z=-104985..-24507";
241    test_part_two!(example => 2_758_514_936_282_235);
242
243    let real_input = include_str!("day22_input.txt");
244    test_part_one!(real_input => 576_028);
245    test_part_two!(real_input => 1_387_966_280_636_636);
246}