-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
208 lines (186 loc) · 5.66 KB
/
main.rs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use itertools::Itertools;
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one::<2_000_000>(data));
println!("Part 2: {}", part_two(data));
}
fn part_one<const ROW: i32>(data: &str) -> i32 {
data.lines()
.map(read_line)
.map(|(s, b)| (s, manhattan_distance(s, b)))
.filter_map(points_on_line::<ROW>)
.sorted_unstable_by_key(|(a, _)| a.0)
.fold(Default::default(), fold_partition)
.items
- 1
}
fn part_two(data: &str) -> u64 {
let p = data
.lines()
.map(read_line)
.map(|(s, b)| (s, manhattan_distance(s, b)))
.combinations(2)
.filter_map(one_separated)
.map(get_separation_line)
.combinations(2)
.find_map(get_intersection)
.unwrap_or(Point(0, 0));
//println!("{p:?}");
let x = u64::try_from(p.0).unwrap();
let y = u64::try_from(p.1).unwrap();
x * 4_000_000 + y
}
#[derive(Debug)]
struct FoldAcc {
partition: (i32, i32),
items: i32,
}
impl Default for FoldAcc {
fn default() -> Self {
FoldAcc {
partition: (i32::MIN, i32::MIN),
items: 0,
}
}
}
fn fold_partition(accumulator: FoldAcc, (a, b): (Point, Point)) -> FoldAcc {
if b.0 <= accumulator.partition.1 {
return accumulator;
}
if a.0 > accumulator.partition.1 + 1 {
return FoldAcc {
partition: (a.0, b.0),
items: accumulator.items + b.0 - a.0 + 1,
};
}
FoldAcc {
partition: (accumulator.partition.0, b.0),
items: accumulator.items + b.0 - accumulator.partition.1,
}
}
fn one_separated(combination: Vec<(Point, u32)>) -> Option<((Point, u32), (Point, u32))> {
let (s1, d1) = combination[0];
let (s2, d2) = combination[1];
if manhattan_distance(s1, s2) == d1 + d2 + 2 {
Some(((s1, d1), (s2, d2)))
} else {
None
}
}
fn get_separation_line(((s1, d1), (s2, d2)): ((Point, u32), (Point, u32))) -> (Point, Point) {
let d1 = i32::try_from(d1).unwrap() + 1;
let d2 = i32::try_from(d2).unwrap() + 1;
let top: Point;
let bottom: Point;
match (s1.0 < s2.0, s1.1 < s2.1) {
(true, true) => {
if s1.1 + d1 < s2.1 {
top = Point(s1.0, s1.1 + d1);
} else {
top = Point(s2.0 - d2, s2.1);
}
if s1.0 + d1 < s2.0 {
bottom = Point(s1.0 + d1, s1.1);
} else {
bottom = Point(s2.0, s2.1 - d2);
}
}
(false, true) => {
if s1.1 + d1 < s2.1 {
top = Point(s1.0, s1.1 + d1);
} else {
top = Point(s2.0 - d2, s2.1);
}
if s1.0 - d1 > s2.0 {
bottom = Point(s1.0 - d1, s1.1);
} else {
bottom = Point(s2.0, s2.1 - d2);
}
}
(true, false) => {
if s1.0 + d1 < s2.0 {
top = Point(s1.0 + d1, s1.1);
} else {
top = Point(s2.0, s2.1 + d2);
}
if s1.1 - d1 > s2.1 {
bottom = Point(s1.0, s1.1 - d1);
} else {
bottom = Point(s2.0 - d2, s2.1);
}
}
(false, false) => {
if s1.0 - d1 > s2.0 {
top = Point(s1.0 - d1, s1.1);
} else {
top = Point(s2.0, s2.1 + d2);
}
if s1.1 - d1 > s2.0 {
bottom = Point(s1.0, s1.1 - d1);
} else {
bottom = Point(s2.0 - d2, s2.1);
}
}
}
(top, bottom)
}
fn get_intersection(combination: Vec<(Point, Point)>) -> Option<Point> {
let line_1 = combination[0];
let line_2 = combination[1];
let s1 = slope(line_1);
let s2 = slope(line_2);
if s1 == s2 {
return None;
}
let mult = ((line_1.0 .0 - line_2.0 .0) * s1 - (line_2.0 .1 - line_1.0 .1)) / 2;
if mult < 0 || mult > (line_1.0 .0 - line_1.1 .0).abs() {
return None;
}
Some(Point(line_1.0 .0 - s1 * mult, line_1.0 .1 - mult))
}
fn slope(line: (Point, Point)) -> i32 {
(line.0 .0 - line.1 .0) / (line.0 .1 - line.1 .1)
}
fn read_line(line: &str) -> (Point, Point) {
let (sensor, beacon) = line.split_once(':').unwrap();
let sensor = sensor.trim_start_matches("Sensor at ");
let (x, y) = sensor.split_once(", ").unwrap();
let x = x.trim_start_matches("x=").parse().unwrap();
let y = y.trim_start_matches("y=").parse().unwrap();
let sensor = Point(x, y);
let beacon = beacon.trim_start_matches(" closest beacon is at ");
let (x, y) = beacon.split_once(", ").unwrap();
let x = x.trim_start_matches("x=").parse().unwrap();
let y = y.trim_start_matches("y=").parse().unwrap();
let beacon = Point(x, y);
(sensor, beacon)
}
fn manhattan_distance(a: Point, b: Point) -> u32 {
(a.0 - b.0).unsigned_abs() + (a.1 - b.1).unsigned_abs()
}
fn points_on_line<const ROW: i32>((source, size): (Point, u32)) -> Option<(Point, Point)> {
let y_diff = (source.1 - ROW).unsigned_abs();
if y_diff > size {
return None;
}
let rem = i32::try_from(size - y_diff).unwrap();
let point_left = Point(source.0 - rem, ROW);
let point_right = Point(source.0 + rem, ROW);
Some((point_left, point_right))
}
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
struct Point(i32, i32);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(26, part_one::<10>(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(56000011, part_two(data));
}
}