Skip to content

Commit 34903c8

Browse files
committed
Day 11 Done
1 parent 19a4710 commit 34903c8

5 files changed

Lines changed: 148 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1010

1111
| Year | Completed |
1212
| :---: | :---: |
13-
| [2025](aoc2025) | 20/24 |
13+
| [2025](aoc2025) | 22/24 |
1414
| [2024](aoc2024) | 50/50 |
1515
| [2023](aoc2023) | 50/50 |
1616
| [2015](aoc2015) | 24/50 |

aoc2025/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1616
| [Day 8](https://adventofcode.com/2025/day/8) | [code](src/bin/08.rs) |||
1717
| [Day 9](https://adventofcode.com/2025/day/9) | [code](src/bin/09.rs) |||
1818
| [Day 10](https://adventofcode.com/2025/day/10) | [code](src/bin/10.rs) |||
19-
| [Day 11](https://adventofcode.com/2025/day/11) | [code](src/bin/11.rs) | _ | _ |
19+
| [Day 11](https://adventofcode.com/2025/day/11) | [code](src/bin/11.rs) | | |
2020
| [Day 12](https://adventofcode.com/2025/day/12) | [code](src/bin/12.rs) | _ | _ |
2121

2222
---

aoc2025/data/examples/11-2.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
svr: aaa bbb
2+
aaa: fft
3+
fft: ccc
4+
bbb: tty
5+
tty: ccc
6+
ccc: ddd eee
7+
ddd: hub
8+
hub: fff
9+
eee: dac
10+
dac: fff
11+
fff: ggg hhh
12+
ggg: out
13+
hhh: out

aoc2025/data/examples/11.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
aaa: you hhh
2+
you: bbb ccc
3+
bbb: ddd eee
4+
ccc: ddd eee fff
5+
ddd: ggg
6+
eee: out
7+
fff: out
8+
ggg: out
9+
hhh: ccc fff iii
10+
iii: out

aoc2025/src/bin/11.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use std::collections::HashMap;
2+
3+
use itertools::Itertools;
4+
5+
advent_of_code::solution!(11);
6+
7+
fn parse_input(input: &str) -> (HashMap<usize, Vec<usize>>, HashMap<String, usize>) {
8+
let mut machine_ids = HashMap::new();
9+
let s_to_id = move |m: String, machine_ids: &mut HashMap<String, usize>| {
10+
if let Some(x) = machine_ids.get(&m) {
11+
*x
12+
} else {
13+
let curr = machine_ids.len();
14+
machine_ids.insert(m, curr);
15+
curr
16+
}
17+
};
18+
let mut machines = HashMap::new();
19+
input.lines().for_each(|s| {
20+
let mut parts = s.split_whitespace();
21+
let machine = parts
22+
.next()
23+
.expect("Should find machine")
24+
.trim_end_matches(':')
25+
.to_string();
26+
let m = s_to_id(machine, &mut machine_ids);
27+
let outputs = parts
28+
.map(|x| s_to_id(x.to_string(), &mut machine_ids))
29+
.collect_vec();
30+
machines.insert(m, outputs);
31+
});
32+
(machines, machine_ids)
33+
}
34+
35+
fn recurse_paths(
36+
m: usize,
37+
end: usize,
38+
machines: &HashMap<usize, Vec<usize>>,
39+
memo: &mut HashMap<usize, u64>,
40+
) -> u64 {
41+
if m == end {
42+
return 1;
43+
}
44+
if let Some(x) = memo.get(&m) {
45+
return *x;
46+
}
47+
let mut total = 0;
48+
for x in machines.get(&m).expect("All outputs should be inputs") {
49+
total += recurse_paths(*x, end, machines, memo);
50+
}
51+
memo.insert(m, total);
52+
total
53+
}
54+
55+
pub fn part_one(input: &str) -> Option<u64> {
56+
let (machines, machine_ids) = parse_input(input);
57+
let start = machine_ids.get("you").unwrap();
58+
let end = machine_ids.get("out").unwrap();
59+
let mut seen = HashMap::new();
60+
let total = recurse_paths(*start, *end, &machines, &mut seen);
61+
Some(total)
62+
}
63+
64+
fn recurse_paths_part2(
65+
m: usize,
66+
end: usize,
67+
dac: usize,
68+
fft: usize,
69+
mut seen_dac: bool,
70+
mut seen_fft: bool,
71+
machines: &HashMap<usize, Vec<usize>>,
72+
memo: &mut HashMap<(usize, bool, bool), u64>,
73+
) -> u64 {
74+
if m == end {
75+
return if seen_dac && seen_fft { 1 } else { 0 };
76+
}
77+
if m == dac {
78+
seen_dac = true;
79+
}
80+
if m == fft {
81+
seen_fft = true;
82+
}
83+
if let Some(x) = memo.get(&(m, seen_dac, seen_fft)) {
84+
return *x;
85+
}
86+
87+
let mut total = 0;
88+
for x in machines.get(&m).expect("All outputs should be inputs") {
89+
total += recurse_paths_part2(*x, end, dac, fft, seen_dac, seen_fft, machines, memo);
90+
}
91+
memo.insert((m, seen_dac, seen_fft), total);
92+
total
93+
}
94+
95+
pub fn part_two(input: &str) -> Option<u64> {
96+
let (machines, machine_ids) = parse_input(input);
97+
let start = machine_ids.get("svr").unwrap();
98+
let end = machine_ids.get("out").unwrap();
99+
let dac = machine_ids.get("dac").unwrap();
100+
let fft = machine_ids.get("fft").unwrap();
101+
let mut seen = HashMap::new();
102+
let total = recurse_paths_part2(*start, *end, *dac, *fft, false, false, &machines, &mut seen);
103+
Some(total)
104+
}
105+
106+
#[cfg(test)]
107+
mod tests {
108+
use super::*;
109+
110+
#[test]
111+
fn test_part_one() {
112+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
113+
assert_eq!(result, Some(5));
114+
}
115+
116+
#[test]
117+
fn test_part_two() {
118+
let result = part_two(&advent_of_code::template::read_file_part(
119+
"examples", DAY, 2,
120+
));
121+
assert_eq!(result, Some(2));
122+
}
123+
}

0 commit comments

Comments
 (0)