Skip to content

Commit e2e82e2

Browse files
committed
add day 12
1 parent 893ac53 commit e2e82e2

File tree

5 files changed

+168
-2
lines changed

5 files changed

+168
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
input??

Cargo.lock

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ edition = "2021"
77

88
[dependencies]
99
clap = { version = "4.0.29", features = ["derive"] }
10+
pathfinding = "4.0.0"
1011
regex = "1.7.0"

input12.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Sabqponm
2+
abcryxxl
3+
accszExk
4+
acctuvwj
5+
abdefghi

src/day12.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
1+
use std::convert::identity;
2+
13
use super::*;
4+
use pathfinding::prelude::bfs;
5+
6+
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7+
struct Pos(usize, usize);
8+
9+
impl Pos {
10+
fn successors(&self, map: &Vec<Vec<u8>>) -> Vec<Pos> {
11+
let &Pos(x, y) = self;
12+
let mut result = Vec::new();
13+
let max_target = map[x][y] + 1;
14+
if x > 0 && map[x - 1][y] <= max_target {
15+
result.push(Pos(x - 1, y));
16+
}
17+
if x < map.len() - 1 && map[x + 1][y] <= max_target {
18+
result.push(Pos(x + 1, y));
19+
}
20+
if y > 0 && map[x][y - 1] <= max_target {
21+
result.push(Pos(x, y - 1));
22+
}
23+
if y < map[x].len() - 1 && map[x][y + 1] <= max_target {
24+
result.push(Pos(x, y + 1));
25+
}
26+
result
27+
}
28+
}
229

330
pub fn solve(filename: String) {
431
if let Ok(lines) = read_lines(&filename) {
532
// Consumes the iterator, returns an (Optional) String
33+
let mut map: Vec<Vec<u8>> = Vec::new();
34+
let mut x = 0;
35+
let mut target = Pos(0, 0);
36+
let mut start = Pos(0, 0);
37+
let mut starts: Vec<Pos> = Vec::new();
638
for line in lines {
739
if let Ok(ip) = line {
8-
println!("{}", ip)
40+
map.push(ip.chars().map(|c| c as u8).collect());
41+
map[map.len() - 1].iter().enumerate().for_each(|(y, c)| {
42+
if *c == ('E' as u8) {
43+
target = Pos(x, y);
44+
}
45+
if *c == ('S' as u8) {
46+
start = Pos(x, y);
47+
starts.push(Pos(x, y));
48+
}
49+
if *c == ('a' as u8) {
50+
starts.push(Pos(x, y));
51+
}
52+
});
953
}
54+
x += 1;
1055
}
56+
let Pos(x, y) = target;
57+
map[x][y] = 'z' as u8;
58+
let Pos(x, y) = start;
59+
map[x][y] = 'a' as u8;
60+
let result = bfs(&start, |p| p.successors(&map), |p| *p == target);
61+
println!("{:?}", result.expect("").len() - 1);
62+
let lens = starts
63+
.iter()
64+
.map(|s| bfs(s, |p| p.successors(&map), |p| *p == target))
65+
.filter_map(identity)
66+
.map(|r| r.len() - 1).min();
67+
println!("{:?}", lens)
1168
}
1269
}
13-

0 commit comments

Comments
 (0)