Skip to content

Commit 2eb8dd5

Browse files
committed
chore: WIP of y2025::day_10
1 parent a6a8e1e commit 2eb8dd5

File tree

7 files changed

+163
-3
lines changed

7 files changed

+163
-3
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 2025 --day 9" />
4+
<option name="command" value="run --package aoclp_solutions --bin aoclp_solutions -- --year 2025 --day 10" />
55
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
66
<envs />
77
<option name="emulateTerminal" value="true" />

Cargo.lock

Lines changed: 46 additions & 0 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
@@ -14,5 +14,6 @@ itertools = "0.14.0"
1414
num = "0.4.3"
1515
paste = "1.0.15"
1616
primes = "0.4.0"
17+
rayon = "1.11.0"
1718
regex = "1.12.2"
1819
strum = { version = "0.27.2", features = ["derive"] }

aoclp_solutions/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "aoclp_solutions"
33
version = "0.1.0"
44
edition = "2021"
5-
rust-version = "1.81.0"
5+
rust-version = "1.82.0"
66

77
[dependencies]
88
aoclp = { path = "../aoclp" }
@@ -11,4 +11,5 @@ clap = { workspace = true, features = ["derive"] }
1111
derive-where = { workspace = true }
1212
itertools = { workspace = true }
1313
primes = { workspace = true }
14+
rayon = { workspace = true }
1415
strum = { workspace = true, features = ["derive"] }

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, 10] },
14-
{ 2025, [01, 02, 03, 04, 05, 06, 07, 08, 09] }
14+
{ 2025, [01, 02, 03, 04, 05, 06, 07, 08, 09, 10] }
1515
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use std::cmp::min;
2+
use std::collections::HashMap;
3+
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
4+
use std::str::FromStr;
5+
use std::sync::OnceLock;
6+
use itertools::Itertools;
7+
use aoclp::anyhow::Context;
8+
use aoclp::regex::Regex;
9+
use aoclp::solvers_impl::input::safe_get_input_as_many;
10+
11+
pub fn part_1() -> usize {
12+
input().iter().map(Machine::fewest_presses_for_lights).sum()
13+
}
14+
15+
pub fn part_2() -> usize {
16+
input().par_iter().map(Machine::fewest_presses_for_joltage).sum()
17+
}
18+
19+
#[derive(Debug, Clone)]
20+
struct Machine {
21+
target_lights: Vec<bool>,
22+
button_wirings: Vec<Vec<usize>>,
23+
joltage_reqs: Vec<usize>,
24+
}
25+
26+
impl Machine {
27+
fn fewest_presses_for_lights(&self) -> usize {
28+
let mut presses = 0;
29+
let mut states = vec![vec![false; self.target_lights.len()]];
30+
loop {
31+
if states.iter().contains(&self.target_lights) {
32+
break presses;
33+
}
34+
35+
states = states
36+
.into_iter()
37+
.flat_map(|l| {
38+
self.button_wirings
39+
.iter()
40+
.map(move |wiring| {
41+
let mut next = l.clone();
42+
for w in wiring {
43+
next[*w] = !next[*w]
44+
}
45+
next
46+
})
47+
})
48+
.unique()
49+
.collect();
50+
presses += 1;
51+
}
52+
}
53+
54+
fn fewest_presses_for_joltage(&self) -> usize {
55+
let mut cache = HashMap::new();
56+
let presses = self.fewest_presses_for_joltage_from(vec![0; self.joltage_reqs.len()], 0, usize::MAX, &mut cache);
57+
println!("{presses}");
58+
presses
59+
}
60+
61+
fn fewest_presses_for_joltage_from(&self, cur: Vec<usize>, steps: usize, max_steps: usize, _cache: &mut HashMap<Vec<usize>, usize>) -> usize {
62+
if *cur == self.joltage_reqs {
63+
return steps;
64+
// } else if let Some(presses) = cache.get(&cur) {
65+
// return *presses;
66+
} else if steps == max_steps || cur.iter().zip(self.joltage_reqs.iter()).any(|(cur, req)| *cur > *req) {
67+
return max_steps;
68+
}
69+
70+
let presses = self.button_wirings
71+
.iter()
72+
.fold(max_steps, |max, wiring| {
73+
let mut next = cur.clone();
74+
for w in wiring {
75+
next[*w] += 1;
76+
}
77+
let next_presses = self.fewest_presses_for_joltage_from(next, steps + 1, max, _cache);
78+
min(max, next_presses)
79+
});
80+
// cache.insert(cur, presses);
81+
presses
82+
}
83+
}
84+
85+
impl FromStr for Machine {
86+
type Err = aoclp::Error;
87+
88+
fn from_str(s: &str) -> Result<Self, Self::Err> {
89+
static REGEX: OnceLock<Regex> = OnceLock::new();
90+
let re = REGEX.get_or_init(|| {
91+
Regex::new(r"^\s*\[(?<lights>[.#]+)]\s+(?<buttons>(?:\((?:\d+,?)+\)\s+)+)\s*\{(?<joltage>(?:\d+,?)+)}\s*$").unwrap()
92+
});
93+
94+
let captures = re.captures(s).with_context(|| format!("invalid machine spec: {s}"))?;
95+
let target_lights = &captures["lights"];
96+
let button_wirings = &captures["buttons"];
97+
let joltage_reqs = &captures["joltage"];
98+
99+
let target_lights = target_lights.chars().map(|c| c == '#').collect();
100+
let button_wirings = button_wirings.split_ascii_whitespace().map(|bw| {
101+
bw[1..bw.len() - 1].split(',').map(|l| l.parse::<usize>()).try_collect()
102+
}).try_collect()?;
103+
let joltage_reqs = joltage_reqs.split(',').map(|j| j.parse::<usize>()).try_collect()?;
104+
105+
Ok(Self { target_lights, button_wirings, joltage_reqs })
106+
}
107+
}
108+
109+
fn input() -> Vec<Machine> {
110+
safe_get_input_as_many(2025, 10)
111+
}

aoclp_solutions/src/y2025/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod day_06;
77
pub mod day_07;
88
pub mod day_08;
99
pub mod day_09;
10+
pub mod day_10;

0 commit comments

Comments
 (0)