Skip to content

Commit 595fc85

Browse files
committed
refactor(11/2025): try to solve it using path counting
1 parent 30c5aa6 commit 595fc85

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

src/solutions/year2025/day11.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::solutions::Solution;
22
use crate::utils::graphs::all_paths::AllPaths;
33
use crate::utils::graphs::graph::Graph;
4+
use std::collections::VecDeque;
45

56
pub struct Day11;
67

@@ -22,11 +23,11 @@ impl Solution for Day11 {
2223
let graph = self.parse(input);
2324
let all_paths: AllPaths<&str> = (&graph).into();
2425

26+
let should_count_path =
27+
|path: &VecDeque<&str>| path.contains(&LABEL_DAC) && path.contains(&LABEL_FFT);
28+
2529
all_paths
26-
.paths(LABEL_SVR, LABEL_OUT)
27-
.iter()
28-
.filter(|path| path.contains(&LABEL_DAC) && path.contains(&LABEL_FFT))
29-
.count()
30+
.count_paths(LABEL_SVR, LABEL_OUT, should_count_path)
3031
.to_string()
3132
}
3233
}

src/utils/graphs/all_paths.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::graphs::graph::Graph;
22
use std::collections::{HashMap, HashSet, VecDeque};
3+
use std::fmt::Debug;
34
use std::hash::Hash;
45

56
pub trait IsEnd<T> {
@@ -24,7 +25,7 @@ pub struct AllPaths<'a, T: 'a> {
2425

2526
impl<'a, T> From<&'a Graph<T>> for AllPaths<'a, T>
2627
where
27-
T: Eq + Hash + Copy + PartialEq,
28+
T: Eq + Hash + Copy + PartialEq + Debug,
2829
{
2930
fn from(graph: &'a Graph<T>) -> Self {
3031
Self::new(move |p: T| graph.neighbours(&p))
@@ -33,15 +34,15 @@ where
3334

3435
impl<'a, T> From<&'a HashMap<T, Vec<T>>> for AllPaths<'a, T>
3536
where
36-
T: Eq + Hash + Copy + PartialEq,
37+
T: Eq + Hash + Copy + PartialEq + Debug,
3738
{
3839
fn from(value: &'a HashMap<T, Vec<T>>) -> Self {
3940
Self::new(move |p: T| value.get(&p).unwrap().to_vec())
4041
}
4142
}
4243
impl<'a, T> AllPaths<'a, T>
4344
where
44-
T: Eq + Hash + Copy,
45+
T: Eq + Hash + Copy + Debug,
4546
{
4647
pub fn new(adjacency: impl Fn(T) -> Vec<T> + 'a) -> Self {
4748
Self {
@@ -88,6 +89,56 @@ where
8889
path.pop_back();
8990
visited.remove(&from);
9091
}
92+
93+
pub fn count_paths<E>(
94+
&self,
95+
start: T,
96+
end: E,
97+
should_count_path: impl Fn(&VecDeque<T>) -> bool,
98+
) -> usize
99+
where
100+
E: IsEnd<T>,
101+
{
102+
let mut visited = HashSet::new();
103+
let mut path = VecDeque::new();
104+
105+
self.visit_and_count(start, &end, &mut visited, &mut path, &should_count_path)
106+
}
107+
108+
fn visit_and_count<E, F>(
109+
&self,
110+
from: T,
111+
end: &E,
112+
visited: &mut HashSet<T>,
113+
path: &mut VecDeque<T>,
114+
should_count_path: &F,
115+
) -> usize
116+
where
117+
E: IsEnd<T>,
118+
F: Fn(&VecDeque<T>) -> bool,
119+
{
120+
let mut count = 0usize;
121+
122+
visited.insert(from); // probably is not needed for graph
123+
path.push_back(from);
124+
125+
if end.is_end(&from) {
126+
if should_count_path(path) {
127+
count += 1;
128+
}
129+
} else {
130+
for p in (self.adjacency)(from) {
131+
if !visited.contains(&p) {
132+
count += self.visit_and_count(p, end, visited, path, should_count_path);
133+
}
134+
}
135+
}
136+
137+
path.pop_back();
138+
visited.remove(&from);
139+
140+
count
141+
}
91142
}
92143

93144
#[cfg(test)]

src/utils/graphs/longest_path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::utils::graphs::all_paths::AllPaths;
22
use hash::Hash;
33
use itertools::Itertools;
44
use std::collections::VecDeque;
5+
use std::fmt::Debug;
56
use std::hash;
67

78
pub struct LongestPath<'a, T> {
@@ -11,7 +12,7 @@ pub struct LongestPath<'a, T> {
1112

1213
impl<'a, T> LongestPath<'a, T>
1314
where
14-
T: PartialEq + Clone + Copy + Eq + Hash,
15+
T: PartialEq + Clone + Copy + Eq + Hash + Debug,
1516
{
1617
pub fn new(adjacency: &'a dyn Fn(T) -> Vec<T>, cost: &'a dyn Fn(T, T) -> usize) -> Self {
1718
Self { adjacency, cost }

0 commit comments

Comments
 (0)