Skip to content

Commit cd3101a

Browse files
committed
solve: day11
1 parent b321814 commit cd3101a

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
resolver = "2"
33
members = [
44
"aoc_lib/aoc_derive",
5-
"aoc_lib/utils", "day1", "day10", "day2", "day3", "day4", "day5", "day6", "day7", "day8", "day9",
5+
"aoc_lib/utils", "day1", "day10", "day11", "day2", "day3", "day4", "day5", "day6", "day7", "day8", "day9",
66
]
77

88
[workspace.dependencies]

day11/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "day11"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
aoc_derive.path = '../aoc_lib/aoc_derive'
8+
utils.path = '../aoc_lib/utils'
9+
derive_more.workspace = true
10+
itertools.workspace = true
11+
lazy-regex.workspace = true
12+
parse-display.workspace = true
13+
rayon.workspace = true
14+
regex.workspace = true
15+
num.workspace = true
16+
17+
[dev-dependencies]
18+
pretty_assertions.workspace = true

day11/src/main.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use std::collections::HashMap;
2+
3+
use aoc_derive::aoc_main;
4+
use itertools::Itertools;
5+
use utils::*;
6+
7+
#[derive(Clone, Debug)]
8+
struct Node<'a> {
9+
label: &'a str,
10+
next: Vec<&'a str>,
11+
}
12+
13+
impl<'a> Node<'a> {
14+
fn find_out(&'a self, graph: &HashMap<&'a str, Node>) -> usize {
15+
self.next.iter().map(move |&n| if n == "out" { 1 } else { graph[n].find_out(graph) }).sum()
16+
}
17+
18+
fn find_out2(
19+
&'a self,
20+
graph: &'a HashMap<&'a str, Node>,
21+
dac: bool,
22+
fft: bool,
23+
cache: &mut HashMap<(&'a str, bool, bool), usize>,
24+
) -> usize {
25+
if let Some(cached) = cache.get(&(self.label, dac, fft)) {
26+
return *cached;
27+
}
28+
let res = self
29+
.next
30+
.iter()
31+
.map(|&n| {
32+
if n == "out" {
33+
if dac && fft { 1 } else { 0 }
34+
} else {
35+
graph[n].find_out2(graph, dac || n == "dac", fft || n == "fft", cache)
36+
}
37+
})
38+
.sum();
39+
40+
cache.insert((self.label, dac, fft), res);
41+
42+
res
43+
}
44+
}
45+
46+
#[aoc_main]
47+
fn solve(input: Input) -> impl Into<Solution> {
48+
let graph: HashMap<_, _> = input
49+
.lines()
50+
.map(|line| {
51+
let (label, rhs) = line.split(':').collect_tuple().unwrap();
52+
(label, Node { label, next: rhs.split_whitespace().collect() })
53+
})
54+
.collect();
55+
56+
let start = graph.iter().find_map(|(&label, node)| (label == "you").then_some(node));
57+
let server = graph.iter().find_map(|(&label, node)| (label == "svr").then_some(node));
58+
(
59+
start.map(|start| start.find_out(&graph)).unwrap_or_default(),
60+
server
61+
.map(|server| server.find_out2(&graph, false, false, &mut HashMap::new()))
62+
.unwrap_or_default(),
63+
)
64+
}
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
#[test]
70+
fn test_examples() {
71+
use utils::assert_example;
72+
assert_example!(
73+
r#"aaa: you hhh
74+
you: bbb ccc
75+
bbb: ddd eee
76+
ccc: ddd eee fff
77+
ddd: ggg
78+
eee: out
79+
fff: out
80+
ggg: out
81+
hhh: ccc fff iii
82+
iii: out"#,
83+
5
84+
);
85+
86+
assert_part2!(
87+
r#"svr: aaa bbb
88+
aaa: fft
89+
fft: ccc
90+
bbb: tty
91+
tty: ccc
92+
ccc: ddd eee
93+
ddd: hub
94+
hub: fff
95+
eee: dac
96+
dac: fff
97+
fff: ggg hhh
98+
ggg: out
99+
hhh: out"#,
100+
2
101+
);
102+
}
103+
}

0 commit comments

Comments
 (0)