Skip to content

Commit 399763b

Browse files
committed
Merge branch 'main' into day10
2 parents 03221a4 + 1fb5edd commit 399763b

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

aoc2025_wasm/inputs

aoc2025_wasm/src/impl_2025_10.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl FromStr for InputLine {
3030
.map(|c| c == b'#')
3131
.collect::<Vec<_>>();
3232

33-
let mut wiring: Vec<Vec<usize>> = groups
33+
let wiring: Vec<Vec<usize>> = groups
3434
.get(1..groups.len() - 1)
3535
.context("not enough groups")?
3636
.iter()
@@ -42,8 +42,6 @@ impl FromStr for InputLine {
4242
})
4343
.collect::<Result<_, _>>()?;
4444

45-
wiring.sort_unstable_by_key(|w| Reverse(w.len()));
46-
4745
let joltages = groups
4846
.last()
4947
.context("not enough groups on line")?

aoc2025_wasm/src/impl_2025_11.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use anyhow::{Context, Result};
2+
use std::collections::HashMap;
3+
4+
fn connections_from_line(inp: &str) -> Result<Vec<(&str, &str)>> {
5+
let (here, there) = inp.split_once(": ").context("bad format")?;
6+
7+
Ok(there
8+
.split(" ")
9+
.flat_map(|x| [(here, x)])
10+
.collect::<Vec<_>>())
11+
}
12+
13+
fn pathfind(connections: &[(&str, &str)], start: &str, end: &str) -> u64 {
14+
let mut map = HashMap::<&str, u64>::default();
15+
map.insert(start, 1);
16+
17+
let mut ans = 0;
18+
19+
for _ in 0..connections.len() {
20+
let mut new = HashMap::<&str, u64>::default();
21+
22+
connections.iter().for_each(|(s, e)| {
23+
if let Some(oldval) = map.get(s) {
24+
*new.entry(e).or_insert(0) += oldval;
25+
}
26+
});
27+
28+
ans += *new.get(end).unwrap_or(&0);
29+
30+
std::mem::swap(&mut new, &mut map);
31+
}
32+
33+
ans
34+
}
35+
36+
fn parse(inp: &str) -> Result<Vec<(&str, &str)>> {
37+
let connections = inp
38+
.trim()
39+
.lines()
40+
.map(connections_from_line)
41+
.collect::<Result<Vec<_>>>()?;
42+
43+
Ok(connections.into_iter().flatten().collect::<Vec<_>>())
44+
}
45+
46+
fn calculate_p1(connections: &[(&str, &str)]) -> u64 {
47+
pathfind(connections, "you", "out")
48+
}
49+
50+
fn calculate_p2(connections: &[(&str, &str)]) -> u64 {
51+
(pathfind(connections, "svr", "dac")
52+
* pathfind(connections, "dac", "fft")
53+
* pathfind(connections, "fft", "out"))
54+
+ (pathfind(connections, "svr", "fft")
55+
* pathfind(connections, "fft", "dac")
56+
* pathfind(connections, "dac", "out"))
57+
}
58+
59+
pub fn run_2025_11(inp: &str) -> Result<String> {
60+
let connections = parse(inp)?;
61+
let p1 = calculate_p1(&connections);
62+
let p2 = calculate_p2(&connections);
63+
Ok(format!("{p1}\n{p2}"))
64+
}
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
70+
const EXAMPLE_DATA: &str = include_str!("../inputs/examples/2025_11");
71+
const EXAMPLE_DATA_P2: &str = include_str!("../inputs/examples/2025_11_2");
72+
const REAL_DATA: &str = include_str!("../inputs/real/2025_11");
73+
74+
#[test]
75+
fn test_example_p1() {
76+
assert_eq!(calculate_p1(&parse(EXAMPLE_DATA).unwrap()), 5);
77+
}
78+
79+
#[test]
80+
fn test_example_p2() {
81+
assert_eq!(calculate_p2(&parse(EXAMPLE_DATA_P2).unwrap()), 2);
82+
}
83+
84+
#[test]
85+
fn test_real_p1() {
86+
assert_eq!(calculate_p1(&parse(REAL_DATA).unwrap()), 428);
87+
}
88+
89+
#[test]
90+
fn test_real_p2() {
91+
assert_eq!(calculate_p2(&parse(REAL_DATA).unwrap()), 331468292364745);
92+
}
93+
}

aoc2025_wasm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod impl_2025_07;
99
mod impl_2025_08;
1010
mod impl_2025_09;
1111
mod impl_2025_10;
12+
mod impl_2025_11;
1213
pub(crate) mod union_find;
1314

1415
pub use wasm_bindgen_rayon::init_thread_pool;
@@ -29,6 +30,7 @@ pub fn run_day(day: u32, input: &str) -> String {
2930
8 => impl_2025_08::run_2025_08(input),
3031
9 => impl_2025_09::run_2025_09(input),
3132
10 => impl_2025_10::run_2025_10(input),
33+
11 => impl_2025_11::run_2025_11(input),
3234
_ => Err(anyhow!("No solution for that day yet")),
3335
};
3436

0 commit comments

Comments
 (0)