Skip to content

Commit 3189115

Browse files
committed
chore: y2025::day_07
1 parent 0e4e731 commit 3189115

File tree

7 files changed

+121
-24
lines changed

7 files changed

+121
-24
lines changed

.run/run_aoc.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="run_aoc" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
33
<option name="buildProfileId" value="dev" />
4-
<option name="command" value="run --package aoclp_solutions --bin aoclp_solutions -- --year 2024 --day 9" />
4+
<option name="command" value="run --package aoclp_solutions --bin aoclp_solutions -- --year 2025 --day 7" />
55
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
66
<envs />
77
<option name="emulateTerminal" value="true" />

Cargo.lock

Lines changed: 12 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ anyhow = "1.0.100"
88
aocf = "0.1.21"
99
bit-vec = "0.8.0"
1010
clap = { version = "4.5.53", features = ["derive"] }
11+
derive-where = "1.6.0"
1112
fancy-regex = "0.16.2"
1213
itertools = "0.14.0"
1314
num = "0.4.3"

aoclp_solutions/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ rust-version = "1.81.0"
88
aoclp = { path = "../aoclp" }
99
bit-vec = { workspace = true }
1010
clap = { workspace = true, features = ["derive"] }
11-
fancy-regex = { workspace = true }
11+
derive-where = { workspace = true }
1212
itertools = { workspace = true }
1313
paste = { workspace = true }
1414
primes = { workspace = true }

aoclp_solutions/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pub mod y2025;
1111
build_solvers! {
1212
{ 2017, [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] },
1313
{ 2024, [01, 02, 03, 04, 05, 06, 07, 08, 09] },
14-
{ 2025, [01, 02, 03, 04, 05, 06] }
14+
{ 2025, [01, 02, 03, 04, 05, 06, 07] }
1515
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use std::collections::HashMap;
2+
3+
use aoclp::positioning::direction::four_points::Direction4;
4+
use aoclp::positioning::pt::{matrix_to_map, Pt};
5+
use aoclp::solvers_impl::input::safe_get_input_as_terrain;
6+
use derive_where::derive_where;
7+
use itertools::Itertools;
8+
9+
pub fn part_1() -> usize {
10+
manifoldize().splits
11+
}
12+
13+
pub fn part_2() -> usize {
14+
manifoldize().worlds
15+
}
16+
17+
#[derive(Debug, Clone)]
18+
struct Manifold {
19+
parts: HashMap<Pt, char>,
20+
starting_point: Pt,
21+
}
22+
23+
impl Default for Manifold {
24+
fn default() -> Self {
25+
let parts = matrix_to_map(input());
26+
let starting_point = *parts.iter().find(|(_, c)| **c == 'S').unwrap().0;
27+
Self { parts, starting_point }
28+
}
29+
}
30+
31+
#[derive(Debug, Copy, Clone)]
32+
#[derive_where(PartialEq, Eq, PartialOrd, Ord, Hash)]
33+
struct Particle {
34+
pos: Pt,
35+
#[derive_where(skip)]
36+
worlds: usize,
37+
}
38+
39+
impl Particle {
40+
fn initial(manifold: &Manifold) -> Self {
41+
Self { pos: manifold.starting_point, worlds: 1 }
42+
}
43+
44+
fn travel(self, dir: Direction4) -> Self {
45+
Self { pos: self.pos + dir, ..self }
46+
}
47+
48+
fn onward(self, manifold: &Manifold) -> Vec<Self> {
49+
let next = self.travel(Direction4::Down);
50+
if !manifold.parts.contains_key(&next.pos) {
51+
return vec![self];
52+
}
53+
54+
if manifold.parts.get(&next.pos).is_some_and(|c| *c == '^') {
55+
vec![next.travel(Direction4::Left), next.travel(Direction4::Right)]
56+
} else {
57+
vec![next]
58+
}
59+
}
60+
}
61+
62+
struct ManifoldizationResult {
63+
splits: usize,
64+
worlds: usize,
65+
}
66+
67+
fn manifoldize() -> ManifoldizationResult {
68+
let manifold = Manifold::default();
69+
let mut particles = vec![Particle::initial(&manifold)];
70+
71+
let mut splits = 0;
72+
loop {
73+
let new_particles = particles
74+
.iter()
75+
.flat_map(|particle| {
76+
let moved = particle.onward(&manifold);
77+
if moved.len() > 1 {
78+
splits += 1;
79+
}
80+
moved
81+
})
82+
.sorted()
83+
.coalesce(|a, b| {
84+
if a == b {
85+
Ok(Particle { pos: a.pos, worlds: a.worlds + b.worlds })
86+
} else {
87+
Err((a, b))
88+
}
89+
})
90+
.collect_vec();
91+
92+
if new_particles == particles {
93+
break;
94+
}
95+
particles = new_particles;
96+
}
97+
98+
let worlds = particles.into_iter().map(|p| p.worlds).sum();
99+
ManifoldizationResult { splits, worlds }
100+
}
101+
102+
fn input() -> Vec<Vec<char>> {
103+
safe_get_input_as_terrain(2025, 7)
104+
}

aoclp_solutions/src/y2025/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod day_03;
44
pub mod day_04;
55
pub mod day_05;
66
pub mod day_06;
7+
pub mod day_07;

0 commit comments

Comments
 (0)