Skip to content

Commit e724d17

Browse files
committed
feat(18/2024): introduce a start builder
1 parent 2282c0c commit e724d17

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/solutions/year2024/day18.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::solutions::Solution;
2-
use crate::utils::graphs::a_star::AStar;
2+
use crate::utils::graphs::a_star::AStarBuilder;
33
use crate::utils::point::Point;
44
use crate::utils::surface_range::SurfaceRange;
55
use itertools::Itertools;
@@ -34,7 +34,9 @@ impl Solution for Day18 {
3434

3535
let distance = |from: Point, to: Point| from.manhattan_distance(&to) as usize;
3636

37-
let a_star = AStar::new(&neighbours, &distance);
37+
let a_star = AStarBuilder::init(&neighbours, &distance)
38+
.memory_size(self.surface.area())
39+
.build();
3840

3941
(a_star.path(start, end).unwrap().len() - 1).to_string()
4042
}

src/utils/graphs/a_star.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,30 @@ use std::hash::Hash;
66
pub struct AStar<'a, T> {
77
neighbours: &'a dyn Fn(T) -> Vec<T>,
88
distance: &'a dyn Fn(T, T) -> usize,
9+
memory_size: usize,
910
}
1011

1112
impl<'a, T> AStar<'a, T> {
12-
pub fn new(neighbours: &'a dyn Fn(T) -> Vec<T>, distance: &'a dyn Fn(T, T) -> usize) -> Self {
13+
pub fn new(
14+
neighbours: &'a dyn Fn(T) -> Vec<T>,
15+
distance: &'a dyn Fn(T, T) -> usize,
16+
memory_size: usize,
17+
) -> Self {
1318
Self {
1419
neighbours,
1520
distance,
21+
memory_size,
1622
}
1723
}
1824

1925
pub fn path(&self, start: T, end: T) -> Option<Vec<T>>
2026
where
2127
T: Hash + Eq + PartialEq + Ord + Copy + Debug,
2228
{
23-
let mut open_set = BinaryHeap::new();
24-
let mut came_from: HashMap<T, T> = HashMap::new();
25-
let mut g_score: HashMap<T, usize> = HashMap::new();
26-
let mut f_score: HashMap<T, usize> = HashMap::new();
29+
let mut open_set = BinaryHeap::with_capacity(self.memory_size);
30+
let mut came_from: HashMap<T, T> = HashMap::with_capacity(self.memory_size);
31+
let mut g_score: HashMap<T, usize> = HashMap::with_capacity(self.memory_size);
32+
let mut f_score: HashMap<T, usize> = HashMap::with_capacity(self.memory_size);
2733

2834
let distance = (self.distance)(start, end);
2935

@@ -76,3 +82,29 @@ impl<'a, T> AStar<'a, T> {
7682
path
7783
}
7884
}
85+
86+
pub struct AStarBuilder<'a, T> {
87+
neighbours: &'a dyn Fn(T) -> Vec<T>,
88+
distance: &'a dyn Fn(T, T) -> usize,
89+
memory_size: Option<usize>,
90+
}
91+
92+
impl<'a, T> AStarBuilder<'a, T> {
93+
pub fn init(neighbours: &'a dyn Fn(T) -> Vec<T>, distance: &'a dyn Fn(T, T) -> usize) -> Self {
94+
Self {
95+
neighbours,
96+
distance,
97+
memory_size: None,
98+
}
99+
}
100+
101+
pub fn memory_size(mut self, memory_size: usize) -> Self {
102+
self.memory_size = Some(memory_size);
103+
104+
self
105+
}
106+
107+
pub fn build(self) -> AStar<'a, T> {
108+
AStar::new(self.neighbours, self.distance, self.memory_size.unwrap())
109+
}
110+
}

0 commit comments

Comments
 (0)