Skip to content

Commit 3fcbc0d

Browse files
committed
2018 day 25
1 parent 7a3a163 commit 3fcbc0d

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed

crates/utils/src/geometry.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! 2D & 3D vector implementations.
1+
//! 2D, 3D and 4D vector implementations.
22
33
use crate::number::{Integer, Number, Signed, UnsignedInteger};
44
use std::fmt::Debug;
@@ -217,6 +217,12 @@ vec_impl! {3, (T, T, T) =>
217217
pub struct Vec3{0 => x, 1 => y, 2 => z}
218218
}
219219

220+
vec_impl! {4, (T, T, T, T) =>
221+
/// Struct representing a 4D vector or point.
222+
#[doc(alias("Vector4", "Point4", "Point4D"))]
223+
pub struct Vec4{0 => x, 1 => y, 2 => z, 3 => w}
224+
}
225+
220226
/// Enum representing the four cardinal directions.
221227
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
222228
#[repr(u8)]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-1,2,2,0
2+
0,0,2,-2
3+
0,0,0,-2
4+
-1,2,0,0
5+
-2,-2,-2,2
6+
3,0,2,-1
7+
-1,3,2,2
8+
-1,0,-1,0
9+
0,2,1,-2
10+
3,0,0,0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1,-1,0,1
2+
2,0,-1,0
3+
3,2,-1,0
4+
0,0,3,1
5+
0,0,-1,-1
6+
2,3,-2,0
7+
-2,2,0,0
8+
2,-2,0,-1
9+
1,-1,0,-1
10+
3,2,0,2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1,-1,-1,-2
2+
-2,-2,0,1
3+
0,2,1,3
4+
-2,3,-2,1
5+
0,2,3,-2
6+
-1,-1,1,-2
7+
0,-2,-1,0
8+
-2,2,3,-1
9+
1,2,2,0
10+
-1,-2,0,-2

crates/year2018/src/day25.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use utils::geometry::Vec4;
2+
use utils::prelude::*;
3+
4+
/// Clustering points within Manhattan distance.
5+
#[derive(Clone, Debug)]
6+
pub struct Day25 {
7+
points: Vec<Vec4<i32>>,
8+
}
9+
10+
impl Day25 {
11+
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
12+
let mut points = parser::i32()
13+
.repeat_n(b',')
14+
.map(Vec4::from)
15+
.parse_lines(input)?;
16+
17+
points.sort_unstable_by_key(|p| p.x);
18+
19+
Ok(Self { points })
20+
}
21+
22+
#[must_use]
23+
pub fn part1(&self) -> u32 {
24+
let mut remaining = self.points.clone();
25+
let mut to_visit = Vec::with_capacity(remaining.len());
26+
let mut constellations = 0;
27+
while let Some(start) = remaining.pop() {
28+
to_visit.push(start);
29+
while let Some(point) = to_visit.pop() {
30+
// Use binary search to only check points in range on the x-axis.
31+
// This requires keeping the remaining points sorted (instead of using swap_remove)
32+
let start = remaining.partition_point(|p| p.x < point.x - 3);
33+
let end = remaining.partition_point(|p| p.x <= point.x + 3);
34+
35+
to_visit.extend(
36+
remaining.extract_if(start..end, |p| p.manhattan_distance_to(point) <= 3),
37+
);
38+
}
39+
constellations += 1;
40+
}
41+
constellations
42+
}
43+
44+
#[must_use]
45+
pub fn part2(&self) -> &'static str {
46+
"🎄"
47+
}
48+
}
49+
50+
examples!(Day25 -> (u32, &'static str) [
51+
{file: "day25_example0.txt", part1: 4},
52+
{file: "day25_example1.txt", part1: 3},
53+
{file: "day25_example2.txt", part1: 8},
54+
]);

crates/year2018/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ utils::year!(2018 => year2018, ${
2828
22 => day22::Day22,
2929
23 => day23::Day23,
3030
24 => day24::Day24,
31+
25 => day25::Day25,
3132
});

0 commit comments

Comments
 (0)