Skip to content

Commit 0608ecf

Browse files
committed
[2021] Day 15: Part a
1 parent dfcbfb3 commit 0608ecf

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

Cargo.lock

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

aoc_2021/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ edition = "2021"
55

66
[dependencies]
77
common = { path = "../common" }
8+
aoc_lib = { path = "../aoc_lib" }
9+
810
hashbrown = "0.13.1"
911
indoc = "2.0.4"
1012
nd_vec = "0.4.0"

aoc_2021/src/day_15.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use std::collections::VecDeque;
2+
3+
use aoc_lib::{direction::Direction, matrix::Matrix};
4+
use common::{Answer, Solution};
5+
use hashbrown::HashMap;
6+
use nd_vec::vector;
7+
8+
type Pos = nd_vec::Vec2<usize>;
9+
10+
pub struct Day15;
11+
12+
impl Solution for Day15 {
13+
fn name(&self) -> &'static str {
14+
"Chiton"
15+
}
16+
17+
fn part_a(&self, input: &str) -> Answer {
18+
let matrix = Matrix::new_chars(input, |chr| chr.to_digit(10).unwrap() as u8);
19+
20+
let mut out = usize::MAX;
21+
let mut visited = HashMap::new();
22+
let mut queue = VecDeque::new();
23+
queue.push_back((vector!(0, 0), 0));
24+
25+
while let Some((pos, cost)) = queue.pop_front() {
26+
if pos == matrix.size - vector!(1, 1) {
27+
out = out.min(cost);
28+
continue;
29+
}
30+
31+
visited.insert(pos, cost);
32+
for dir in Direction::ALL {
33+
if let Some((next, new_cost)) = dir
34+
.try_advance(pos)
35+
.and_then(|x| Some((x, cost + *matrix.get(x)? as usize)))
36+
{
37+
if let Some(prev) = visited.get(&next) {
38+
if *prev <= new_cost {
39+
continue;
40+
}
41+
}
42+
43+
queue.push_back((next, new_cost));
44+
}
45+
}
46+
}
47+
48+
out.into()
49+
}
50+
51+
fn part_b(&self, input: &str) -> Answer {
52+
Answer::Unimplemented
53+
}
54+
}
55+
56+
#[cfg(test)]
57+
mod test {
58+
use common::Solution;
59+
use indoc::indoc;
60+
61+
use super::Day15;
62+
63+
const CASE: &str = indoc! {"
64+
1163751742
65+
1381373672
66+
2136511328
67+
3694931569
68+
7463417111
69+
1319128137
70+
1359912421
71+
3125421639
72+
1293138521
73+
2311944581
74+
"};
75+
76+
#[test]
77+
fn part_a() {
78+
assert_eq!(Day15.part_a(CASE), 40.into());
79+
}
80+
81+
#[test]
82+
fn part_b() {
83+
assert_eq!(Day15.part_b(CASE), ().into());
84+
}
85+
}

aoc_2021/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod day_11;
1414
mod day_12;
1515
mod day_13;
1616
mod day_14;
17+
mod day_15;
1718
// [import_marker]
1819

1920
pub const ALL: &[&dyn Solution] = &[
@@ -31,6 +32,6 @@ pub const ALL: &[&dyn Solution] = &[
3132
&day_12::Day12,
3233
&day_13::Day13,
3334
&day_14::Day14,
34-
&day_12::Day12,
35+
&day_15::Day15,
3536
// [list_marker]
3637
];

0 commit comments

Comments
 (0)