aoc/year2022/day15.rs
1//! # Beacon Exclusion Zone
2use std::ops::Range;
3
4use crate::util::hash::*;
5use crate::util::iter::*;
6use crate::util::parse::*;
7use crate::util::point::*;
8
9pub struct Input {
10 sensor: Point,
11 beacon: Point,
12 manhattan: i32,
13}
14
15pub fn parse(input: &str) -> Vec<Input> {
16 input
17 .iter_signed()
18 .chunk::<4>()
19 .map(|[x1, y1, x2, y2]| {
20 let sensor = Point::new(x1, y1);
21 let beacon = Point::new(x2, y2);
22 Input { sensor, beacon, manhattan: sensor.manhattan(beacon) }
23 })
24 .collect()
25}
26
27/// The example uses y=10 but the real data uses y=2000000, so break out the logic
28/// into a separate function to enable integration testing.
29pub fn part1(input: &[Input]) -> i32 {
30 part1_testable(input, 2_000_000)
31}
32
33/// A beacon cannot be located within the radius of a sensor unless it is the closest beacon.
34///
35/// We first convert each scanner's diamond shaped area into a one-dimensional range at the
36/// specified row. By sorting the ranges, we can quickly calculate the total number of distinct
37/// ranges where another beacon cannot exist, only counting overlapping areas once.
38///
39/// Beacons can also not be located at the same position as another beacon so we then also discount
40/// any beacon located exactly on the specified row.
41pub fn part1_testable(input: &[Input], row: i32) -> i32 {
42 // Converts the "diamond" shaped area of each scanner into a one-dimensional row.
43 // If the scanner's range does not reach the specified row then return `None`.
44 fn build_range(input: &Input, row: i32) -> Option<Range<i32>> {
45 let Input { sensor, manhattan, .. } = input;
46 let extra = manhattan - (sensor.y - row).abs();
47 (extra >= 0).then(|| (sensor.x - extra)..(sensor.x + extra + 1))
48 }
49
50 // Sort the ranges first.
51 let mut ranges: Vec<_> = input.iter().filter_map(|i| build_range(i, row)).collect();
52 ranges.sort_unstable_by_key(|r| r.start);
53
54 let mut total = 0;
55 let mut max = i32::MIN;
56
57 // Only add the part of each range that extends past the end of all previous ranges.
58 // (it's possible that there is no extra length)
59 for Range { start, end } in ranges {
60 total += (end - max.max(start)).max(0);
61 max = max.max(end);
62 }
63
64 // Returns the x position of all beacons that are located on the specified row.
65 let beacons: FastSet<_> =
66 input.iter().filter_map(|i| (i.beacon.y == row).then_some(i.beacon.x)).collect();
67 total - (beacons.len() as i32)
68}
69
70/// Similar to part one, the logic is broken out into a separate function to enable testing.
71pub fn part2(input: &[Input]) -> u64 {
72 part2_testable(input, 4_000_000)
73}
74
75/// The trick to solving this efficiently is to first *rotate* the corners of the diamond
76/// scanner shape by 45 degrees. This transforms them into squares that make it much easier
77/// to find the missing distress beacon.
78///
79/// Of the entire 4000000 by 4000000 area the missing beacon must be located in the only
80/// square area not covered by a scanner.
81pub fn part2_testable(input: &[Input], size: i32) -> u64 {
82 // Rotate points clockwise by 45 degrees, scale by √2 and extend edge by 1.
83 // This transforms each sensor into an axis aligned bounding box.
84 // The distress beacon is located where the top, left, bottom and right
85 // edges of 4 separate bounding boxes intersect.
86 let (top, left, bottom, right): (FastSet<_>, FastSet<_>, FastSet<_>, FastSet<_>) = input
87 .iter()
88 .map(|Input { sensor, manhattan, .. }| {
89 (
90 sensor.x + sensor.y - manhattan - 1,
91 sensor.x - sensor.y - manhattan - 1,
92 sensor.x + sensor.y + manhattan + 1,
93 sensor.x - sensor.y + manhattan + 1,
94 )
95 })
96 .collect();
97
98 let horizontal: Vec<_> = top.intersection(&bottom).copied().collect();
99 let vertical: Vec<_> = left.intersection(&right).copied().collect();
100 let range = 0..=size;
101
102 // Many input files have vertical.len() == 1 and horizontal.len() == 1, which implies exactly
103 // one answer, but this check covers more situations and is not too expensive.
104 for &x in &vertical {
105 for &y in &horizontal {
106 // Rotate intersection point counter-clockwise and scale by 1 / √2
107 // to return to original coordinates.
108 #[expect(clippy::manual_midpoint)]
109 let point = Point::new((x + y) / 2, (y - x) / 2);
110 // As we're mixing overlaps from different boxes there may be some spurious false
111 // positives, so double check all points are within the specified area
112 // and outside the range of all scanners.
113 if range.contains(&point.x)
114 && range.contains(&point.y)
115 && input.iter().all(|i| i.sensor.manhattan(point) > i.manhattan)
116 {
117 return 4_000_000 * (point.x as u64) + (point.y as u64);
118 }
119 }
120 }
121
122 unreachable!()
123}