Skip to content

Commit c8132e7

Browse files
committed
[2025] Solution for Day 11
1 parent 791bcf3 commit c8132e7

File tree

6 files changed

+153
-2
lines changed

6 files changed

+153
-2
lines changed

2025/src/bin/day09.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::process;
55

66
fn main() {
77
let args: Vec<String> = env::args().collect();
8-
if args.len() < 3 {
8+
if args.len() < 2 {
99
println!("Missing required arguments");
1010
process::exit(1);
1111
}

2025/src/bin/day11.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use aoc2025::days::day11::{part1, part2};
2+
use std::env;
3+
use std::fs;
4+
use std::process;
5+
6+
fn main() {
7+
let args: Vec<String> = env::args().collect();
8+
if args.len() < 2 {
9+
println!("Missing required arguments");
10+
process::exit(1);
11+
}
12+
13+
let contents = fs::read_to_string(&args[1]).expect("Something went wrong");
14+
println!("Part 1: {}", part1(&contents));
15+
println!("Part 2: {}", part2(&contents));
16+
}

2025/src/days.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod day06;
77
pub mod day07;
88
pub mod day08;
99
pub mod day09;
10+
pub mod day11;

2025/src/days/day11.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
use std::collections::{HashMap, VecDeque};
2+
3+
pub fn part1(contents: &str) -> i64 {
4+
let graph = parse_input(contents);
5+
let mut count = 0;
6+
let mut queue = VecDeque::new();
7+
queue.push_back("you");
8+
while !queue.is_empty() {
9+
let current = queue.pop_front().unwrap();
10+
if current == "out" {
11+
count += 1;
12+
continue;
13+
}
14+
if let Some(neighbors) = graph.get(current) {
15+
for &neighbor in neighbors {
16+
queue.push_back(neighbor);
17+
}
18+
}
19+
}
20+
count
21+
}
22+
23+
pub fn part2(contents: &str) -> i64 {
24+
let graph = parse_input(contents);
25+
26+
// Determine which path exists between "dac" and "fft"
27+
let paths_dac_fft = count_paths(&graph, "dac", "fft", &mut HashMap::new());
28+
let paths_fft_dac = count_paths(&graph, "fft", "dac", &mut HashMap::new());
29+
30+
if paths_dac_fft > 0 {
31+
// There is a path from "dac" to "fft"
32+
let paths_srv_dac = count_paths(&graph, "svr", "dac", &mut HashMap::new());
33+
let paths_fft_out = count_paths(&graph, "fft", "out", &mut HashMap::new());
34+
paths_dac_fft * paths_fft_out * paths_srv_dac
35+
} else {
36+
// There is a path from "fft" to "dac"
37+
let paths_srv_fft = count_paths(&graph, "svr", "fft", &mut HashMap::new());
38+
let paths_dac_out = count_paths(&graph, "dac", "out", &mut HashMap::new());
39+
paths_fft_dac * paths_dac_out * paths_srv_fft
40+
}
41+
}
42+
43+
fn parse_input(contents: &str) -> HashMap<&str, Vec<&str>> {
44+
let mut graph = HashMap::new();
45+
for l in contents.lines() {
46+
let parts: Vec<&str> = l.split(": ").collect();
47+
let device = parts[0];
48+
let outputs: Vec<&str> = parts[1].split(' ').collect();
49+
graph.insert(device, outputs);
50+
}
51+
graph
52+
}
53+
54+
fn count_paths(
55+
graph: &HashMap<&str, Vec<&str>>,
56+
start: &str,
57+
end: &str,
58+
memo: &mut HashMap<String, i64>,
59+
) -> i64 {
60+
if let Some(&cached) = memo.get(start) {
61+
return cached;
62+
}
63+
64+
if start == end {
65+
return 1;
66+
}
67+
68+
let mut total_paths = 0;
69+
if let Some(neighbors) = graph.get(start) {
70+
for &neighbor in neighbors {
71+
total_paths += count_paths(graph, neighbor, end, memo);
72+
}
73+
}
74+
75+
memo.insert(start.to_string(), total_paths);
76+
total_paths
77+
}
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
#[test]
84+
fn test_part1() {
85+
let input = vec![
86+
"aaa: you hhh",
87+
"you: bbb ccc",
88+
"bbb: ddd eee",
89+
"ccc: ddd eee fff",
90+
"ddd: ggg",
91+
"eee: out",
92+
"fff: out",
93+
"ggg: out",
94+
"hhh: ccc fff iii",
95+
"iii: out",
96+
];
97+
98+
assert_eq!(part1(&input.join("\n")), 5);
99+
}
100+
101+
#[test]
102+
fn test_part2() {
103+
let input = vec![
104+
"svr: aaa bbb",
105+
"aaa: fft",
106+
"fft: ccc",
107+
"bbb: tty",
108+
"tty: ccc",
109+
"ccc: ddd eee",
110+
"ddd: hub",
111+
"hub: fff",
112+
"eee: dac",
113+
"dac: fff",
114+
"fff: ggg hhh",
115+
"ggg: out",
116+
"hhh: out",
117+
];
118+
119+
assert_eq!(part2(&input.join("\n")), 0);
120+
}
121+
}

2025/tests/day11.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use aoc2025::days::day11::{part1, part2};
2+
3+
#[test]
4+
fn test_part1() {
5+
let content = std::include_str!("../../private/inputs/2025/day11.txt");
6+
assert_eq!(part1(&content), 658);
7+
}
8+
9+
#[test]
10+
fn test_part2() {
11+
let content = std::include_str!("../../private/inputs/2025/day11.txt");
12+
assert_eq!(part2(&content), 0);
13+
}

private

0 commit comments

Comments
 (0)