Skip to content

Commit 9bb37ec

Browse files
committed
rust 2023/23
1 parent 24a50d3 commit 9bb37ec

File tree

8 files changed

+181
-140
lines changed

8 files changed

+181
-140
lines changed

rust/2023/src/day23/mod.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use aocutils::{
44
grid::{Grid, Vertexable},
55
vec2::Vec2,
66
};
7-
use pathfinding::directed::dfs::dfs_reach;
7+
use itertools::Itertools;
8+
use pathfinding::directed::{dfs::dfs_reach, dijkstra::dijkstra_all};
89

910
extern crate test;
1011

@@ -30,6 +31,24 @@ impl Vertexable for Vertex {
3031
}
3132
}
3233

34+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
35+
struct State {
36+
coords: Vec2<isize>,
37+
visited: Vec<Vec2<isize>>,
38+
}
39+
40+
impl State {
41+
fn from(coords: Vec2<isize>, visited: Vec<Vec2<isize>>) -> State {
42+
State { coords, visited }
43+
}
44+
}
45+
46+
impl Display for State {
47+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48+
write!(f, "[coords: {}, visited: {:?}]", self.coords, self.visited)
49+
}
50+
}
51+
3352
fn get_grid(input: &str) -> Grid {
3453
let lines: Vec<Vec<char>> = input
3554
.split_whitespace()
@@ -46,7 +65,7 @@ fn get_grid(input: &str) -> Grid {
4665
grid
4766
}
4867

49-
fn get_start(input: &str) -> Vec2<isize> {
68+
fn get_start(input: &str) -> State {
5069
let x = input
5170
.lines()
5271
.next()
@@ -55,67 +74,50 @@ fn get_start(input: &str) -> Vec2<isize> {
5574
.unwrap()
5675
.0
5776
.len();
58-
Vec2::from((x as isize, 0))
59-
}
60-
61-
pub fn dfs<N, FN, IN>(start: N, successors: FN) -> Vec<Vec2<isize>>
62-
where
63-
FN: FnMut(N) -> IN,
64-
IN: IntoIterator<Item = N>,
65-
{
66-
let v = Vec::new();
67-
v
77+
let current: Vec2<isize> = Vec2::from((x as isize, 0));
78+
State {
79+
coords: current,
80+
visited: Vec::new(),
81+
}
6882
}
6983

7084
pub fn solve_a(input: &str) -> u64 {
7185
let grid = get_grid(input);
7286
let start = get_start(input);
73-
let s = dfs(start, |state| {
74-
let mut next: Vec<Vec2<isize>> = Vec::new();
75-
let steps: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
76-
for step in steps {
77-
let proposed = state + Vec2::from(step);
78-
if !grid.is_inside(&proposed.raw()) {
87+
let s = dfs_reach(start, |state| {
88+
let mut next: Vec<State> = Vec::new();
89+
println!("{}", state.visited.len());
90+
for step in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
91+
let proposed = state.coords + Vec2::from(step);
92+
let mut visited = state.visited.clone();
93+
visited.push(state.coords);
94+
if !grid.is_inside(&proposed.raw()) || visited.contains(&proposed) {
7995
continue;
8096
}
8197
if let Some(v) = grid.get(&proposed.raw()) {
8298
match (v.get_value(), step) {
83-
('.', _) => next.push(proposed),
84-
('>', (1, 0)) => next.push(proposed),
85-
('v', (0, 1)) => next.push(proposed),
86-
('<', (-1, 0)) => next.push(proposed),
87-
('^', (0, -1)) => next.push(proposed),
99+
('.', _) => next.push(State::from(proposed, visited)),
100+
('>', (1, 0)) => next.push(State::from(proposed, visited)),
101+
('v', (0, 1)) => next.push(State::from(proposed, visited)),
102+
('<', (-1, 0)) => next.push(State::from(proposed, visited)),
103+
('^', (0, -1)) => next.push(State::from(proposed, visited)),
88104
_ => continue,
89105
}
90106
}
91107
}
92108
next
93-
});
94-
println!("done");
95-
let longest = s
109+
})
110+
.collect_vec();
111+
println!("dfs done");
112+
let longest: State = s
96113
.into_iter()
97114
.filter(|s| s.coords.y == grid.height - 1)
98-
.next()
99-
// .max_by(|a, b| a.visited.len().cmp(&b.visited.len()))
115+
.max_by(|a, b| a.visited.len().cmp(&b.visited.len()))
100116
.unwrap();
117+
println!("dfs done");
101118
println!("{longest}");
102119

103-
// for y in 0..grid.height {
104-
// for x in 0..grid.width {
105-
// if let Some(v) = grid.get(&(x, y)) {
106-
// if longest.visited.contains(&Vec2::from((x, y))) {
107-
// print!("O");
108-
// } else {
109-
// print!("{}", v.get_value());
110-
// }
111-
// } else {
112-
// print!("#",);
113-
// }
114-
// }
115-
// println!();
116-
// }
117-
118-
let solution = 0;
120+
let solution = longest.visited.len() as u64;
119121
println!("part a: {}", solution);
120122
solution
121123
}

rust/2023/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ fn main() {
2828
// #.....###...###...#...#
2929
// #####################.#
3030
// ";
31+
// let input = "#.##
32+
// #..#
33+
// ##.#
34+
// ";
3135
day23::solve_a(&input);
3236
day23::solve_b(&input);
3337
}

0 commit comments

Comments
 (0)