-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
347 lines (314 loc) · 9.82 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use std::{cmp::Ordering, fmt::Display, str::FromStr};
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> i64 {
let mut h = Horizontals::new(
data.lines()
.map(|l| l.parse::<Instruction>().unwrap())
.map(|ins| (ins.dist, ins.dir)),
);
h.area()
}
fn part_two(data: &str) -> i64 {
let mut h = Horizontals::new(
data.lines()
.map(|l| l.parse::<Instruction>().unwrap().colour),
);
h.area()
}
struct Horizontals {
data: Vec<LocatedInterval>,
}
impl Horizontals {
fn new(source: impl Iterator<Item = (i64, Direction)>) -> Self {
let mut row = 0;
let mut col = 0;
let mut data = Vec::with_capacity(400);
for (dist, dir) in source {
match dir {
Direction::Up => {
row -= dist;
}
Direction::Down => {
row += dist;
}
Direction::Left => {
data.push(LocatedInterval::new((col - dist, col).into(), row));
col -= dist;
}
Direction::Right => {
data.push(LocatedInterval::new((col, col + dist).into(), row));
col += dist;
}
}
}
data.sort_unstable();
Self { data }
}
fn area(&mut self) -> i64 {
let mut area = 0;
let mut verticals = Vec::with_capacity(400);
while let Some(top) = self.data.pop() {
area += self.process_row(&top, 0, &mut verticals);
}
area
}
fn process_row(
&mut self,
top: &LocatedInterval,
skip: usize,
verticals: &mut Vec<LocatedInterval>,
) -> i64 {
if let Some((row_num, row, mut next_skip, Some(intersection))) = self
.data
.iter()
.enumerate()
.rev()
.enumerate()
.skip(skip)
.map(|(next_skip, (row_num, row))| (row_num, row, next_skip, top.intersect_any(row)))
.find(|&(_, _, _, int)| int.is_some())
{
let area = (intersection.to - intersection.from + 1)
* (row.location - top.location + 1)
+ Self::add_verticals(
(top.location, row.location).into(),
intersection.from,
intersection.to,
verticals,
);
// Update our records for what's left of the bottom row we matched to
match row.subtract(intersection) {
(None, None) => {
self.data.remove(row_num);
}
(Some(a), None) => {
let _ = std::mem::replace(&mut self.data[row_num], a);
next_skip += 1;
}
(Some(a), Some(b)) => {
let _ = std::mem::replace(&mut self.data[row_num], a);
self.data.insert(row_num, b);
next_skip += 2;
}
_ => unreachable!(),
}
// Process what's left of the top row we popped off
// If none left, just return the found area
// If some left over, recursively process the residual
match top.subtract(intersection) {
(None, None) => {
return area;
}
(Some(a), None) => {
return area + self.process_row(&a, next_skip, verticals);
}
(Some(a), Some(b)) => {
return area
+ self.process_row(&a, next_skip, verticals)
+ self.process_row(&b, next_skip, verticals);
}
_ => unreachable!(),
}
}
0
}
fn add_verticals(
vert: Interval,
from: i64,
to: i64,
verticals: &mut Vec<LocatedInterval>,
) -> i64 {
let left = LocatedInterval::new(vert, from);
let right = LocatedInterval::new(vert, to);
let double_count_adj = verticals.iter().fold(0, |acc, v| {
if let Some(left_double) = v.intersect(&left) {
return acc - left_double.len();
}
if let Some(right_double) = v.intersect(&right) {
return acc - right_double.len();
}
acc
});
verticals.push(left);
verticals.push(right);
double_count_adj
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Interval {
from: i64,
to: i64,
}
impl Display for Interval {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}, {}]", self.from, self.to)?;
Ok(())
}
}
impl From<(i64, i64)> for Interval {
fn from(value: (i64, i64)) -> Self {
Self {
from: value.0,
to: value.1,
}
}
}
impl Interval {
fn intersect(self, other: Self) -> Option<Self> {
if self.to <= other.from || other.to <= self.from {
return None;
}
let from = std::cmp::max(self.from, other.from);
let to = std::cmp::min(self.to, other.to);
Some((from, to).into())
}
fn subtract(self, other: Self) -> (Option<Self>, Option<Self>) {
match (
self.from.cmp(&other.from),
self.to.cmp(&other.to),
self.from.cmp(&other.to),
self.to.cmp(&other.from),
) {
(_, _, Ordering::Greater | Ordering::Equal, _)
| (_, _, _, Ordering::Less | Ordering::Equal) => (Some(self), None),
(Ordering::Greater | Ordering::Equal, Ordering::Less | Ordering::Equal, _, _) => {
(None, None)
}
(Ordering::Greater | Ordering::Equal, Ordering::Greater, _, _) => {
(Some((other.to, self.to).into()), None)
}
(Ordering::Less, Ordering::Less | Ordering::Equal, _, _) => {
(Some((self.from, other.from).into()), None)
}
(Ordering::Less, Ordering::Greater, _, _) => (
Some((self.from, other.from).into()),
Some((other.to, self.to).into()),
),
}
}
const fn len(self) -> i64 {
self.to - self.from + 1
}
}
#[derive(Debug, PartialEq, Eq)]
struct LocatedInterval {
interval: Interval,
location: i64,
}
impl LocatedInterval {
const fn new(interval: Interval, location: i64) -> Self {
Self { interval, location }
}
fn intersect(&self, other: &Self) -> Option<Interval> {
if self.location != other.location {
return None;
}
self.intersect_any(other)
}
fn intersect_any(&self, other: &Self) -> Option<Interval> {
self.interval.intersect(other.interval)
}
fn subtract(&self, int: Interval) -> (Option<Self>, Option<Self>) {
let (a, b) = self.interval.subtract(int);
(
a.map(|int| Self::new(int, self.location)),
b.map(|int| Self::new(int, self.location)),
)
}
}
impl Display for LocatedInterval {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} @ {}", self.interval, self.location)?;
Ok(())
}
}
impl Ord for LocatedInterval {
fn cmp(&self, other: &Self) -> Ordering {
other.location.cmp(&self.location)
}
}
impl PartialOrd for LocatedInterval {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug)]
struct Instruction {
dir: Direction,
dist: i64,
colour: (i64, Direction),
}
impl FromStr for Instruction {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.split(' ');
let (Some(dir), Some(dist), Some(colour), None) =
(parts.next(), parts.next(), parts.next(), parts.next())
else {
return Err(format!("Cannot split instruction into three parts: {s}"));
};
let dir = dir.parse()?;
let dist = dist
.parse()
.map_err(|_| format!("{dist} cannot be conveted into a number"))?;
let colour = colour.trim_start_matches("(#").trim_end_matches(')');
if colour.len() != 6 {
return Err(format!("The colour {colour} is badly formatted"));
}
let l = i64::from_str_radix(&colour[0..5], 16).map_err(|_| {
format!("Cannot convert the distance part of the colour correctly: {colour}")
})?;
let d = match colour.chars().nth(5) {
Some('0') => Direction::Right,
Some('1') => Direction::Down,
Some('2') => Direction::Left,
Some('3') => Direction::Up,
_ => {
return Err(format!("Final colour instruction is not valid: {colour}"));
}
};
Ok(Self {
dir,
dist,
colour: (l, d),
})
}
}
#[derive(Debug, Clone, Copy)]
enum Direction {
Up,
Down,
Right,
Left,
}
impl FromStr for Direction {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"U" => Ok(Self::Up),
"D" => Ok(Self::Down),
"R" => Ok(Self::Right),
"L" => Ok(Self::Left),
_ => Err(format!("{s} is not a valid direction")),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(62, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(952_408_144_115, part_two(data));
}
}