Skip to content

Commit 5175efc

Browse files
committed
WIP
1 parent 3189115 commit 5175efc

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

aoclp/src/dij.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::collections::{HashMap, HashSet};
2+
use std::hash::Hash;
3+
use std::iter::once;
4+
5+
pub trait Graph<T> {
6+
fn all_nodes(&self) -> impl Iterator<Item = T>;
7+
fn neighbours(&self, node: &T) -> impl Iterator<Item = T>;
8+
fn dist(&self, a: &T, b: &T) -> usize;
9+
fn is_first_better(&self, _a: &T, _b: &T) -> bool {
10+
false
11+
}
12+
}
13+
14+
pub struct Output<T> {
15+
pub dist: HashMap<T, usize>,
16+
pub prev: HashMap<T, T>,
17+
}
18+
19+
pub fn build<T, G>(graph: G, start: T) -> Output<T>
20+
where
21+
T: Clone + Eq + Hash,
22+
G: Graph<T>,
23+
{
24+
let mut q: HashSet<_> = graph
25+
.all_nodes()
26+
.chain(once(start.clone()))
27+
.collect();
28+
let mut dist: HashMap<_, _> = q
29+
.iter()
30+
.map(|n| (n.clone(), usize::MAX))
31+
.collect();
32+
let mut prev = HashMap::new();
33+
dist.insert(start.clone(), 0);
34+
35+
while !q.is_empty() {
36+
let u = q
37+
.iter()
38+
.min_by_key(|n| dist[n])
39+
.cloned()
40+
.unwrap();
41+
if dist[&u] == usize::MAX {
42+
break;
43+
}
44+
45+
q.remove(&u);
46+
graph
47+
.neighbours(&u)
48+
.for_each(|v| {
49+
let alt = dist[&u] + graph.dist(&u, &v);
50+
if alt < dist[&v] || (prev.contains_key(&v) && alt == dist[&v] && graph.is_first_better(&u, &prev[&v])) {
51+
dist.insert(v.clone(), alt);
52+
prev.insert(v, u.clone());
53+
}
54+
});
55+
}
56+
57+
Output { dist, prev }
58+
}
59+
60+
pub fn assemble_path<T>(prev: &HashMap<T, T>, start: &T, end: &T) -> impl Iterator<Item = T>
61+
where
62+
T: Clone + Eq + Hash,
63+
{
64+
let mut path = Vec::new();
65+
let mut n = end.clone();
66+
while n != *start {
67+
path.push(n.clone());
68+
n = match prev.get(&n) {
69+
Some(p) => p.clone(),
70+
None => break,
71+
}
72+
}
73+
path.push(start.clone());
74+
path.into_iter().rev()
75+
}

aoclp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Helper library for Advent of Code.
22
33
pub mod captures;
4+
pub mod dij;
45
pub mod forth;
56
pub mod functional;
67
pub mod looping;

aoclp_solutions/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ pub mod y2025;
1010

1111
build_solvers! {
1212
{ 2017, [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] },
13-
{ 2024, [01, 02, 03, 04, 05, 06, 07, 08, 09] },
13+
{ 2024, [01, 02, 03, 04, 05, 06, 07, 08, 09, 10] },
1414
{ 2025, [01, 02, 03, 04, 05, 06, 07] }
1515
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::collections::HashMap;
2+
use std::ops::Deref;
3+
use aoclp::positioning::pt::Pt;
4+
use aoclp::solvers_impl::input::safe_get_input_as_terrain;
5+
6+
pub fn part_1() -> usize {
7+
0
8+
}
9+
10+
pub fn part_2() -> usize {
11+
0
12+
}
13+
14+
#[derive(Debug, Copy, Clone)]
15+
struct Tile(usize);
16+
17+
impl Deref for Tile {
18+
type Target = usize;
19+
20+
fn deref(&self) -> &Self::Target {
21+
&self.0
22+
}
23+
}
24+
25+
impl From<char> for Tile {
26+
fn from(c: char) -> Self {
27+
Self(c.to_digit(10).unwrap() as usize)
28+
}
29+
}
30+
31+
#[derive(Debug)]
32+
struct Map {
33+
tiles: HashMap<Pt, Tile>,
34+
}
35+
36+
impl Default for Map {
37+
fn default() -> Self {
38+
let map = safe_get_input_as_terrain(2024, 10);
39+
Self { tiles: }
40+
}
41+
}

aoclp_solutions/src/y2024/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod day_06;
77
pub mod day_07;
88
pub mod day_08;
99
pub mod day_09;
10+
pub mod day_10;

0 commit comments

Comments
 (0)