-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
123 lines (111 loc) · 3.08 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Problem: https://adventofcode.com/2022/day/12
use itertools::Itertools;
use std::{
collections::{HashSet, VecDeque},
ops::Add,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Pos(i32, i32);
impl Add<Pos> for Pos {
type Output = Pos;
fn add(self, rhs: Pos) -> Self::Output {
Pos(self.0 + rhs.0, self.1 + rhs.1)
}
}
fn find(grid: &Vec<Vec<u8>>, val: u8) -> Pos {
for row in 0..grid.len() {
for col in 0..grid[0].len() {
if grid[row][col] == val {
return Pos(row as i32, col as i32);
}
}
}
panic!();
}
#[derive(Debug)]
struct Grid {
grid: Vec<Vec<u8>>,
rows: i32,
cols: i32,
start: Pos,
dest: Pos,
}
impl Grid {
fn parse(input: &str) -> Self {
let mut grid: Vec<Vec<u8>> = input
.trim()
.lines()
.map(|l| l.chars().map(|ch| ch.to_string().as_bytes()[0]).collect())
.collect();
let start = find(&grid, b'S');
let dest = find(&grid, b'E');
grid[start.0 as usize][start.1 as usize] = b'a';
grid[dest.0 as usize][dest.1 as usize] = b'z';
let rows = grid.len() as i32;
let cols = grid[0].len() as i32;
Self {
grid,
rows,
cols,
start,
dest,
}
}
fn get(&self, pos: Pos) -> u8 {
self.grid[pos.0 as usize][pos.1 as usize]
}
fn is_valid(&self, pos: Pos) -> bool {
pos.0 >= 0 && pos.1 >= 0 && pos.0 < self.rows && pos.1 < self.cols
}
fn iter(&self) -> impl Iterator<Item = Pos> {
(0..self.rows)
.cartesian_product(0..self.cols)
.map(|(r, c)| Pos(r as i32, c as i32))
}
}
fn find_path(start: Pos, grid: &Grid) -> Option<usize> {
let mut seen = HashSet::<Pos>::new();
let mut queue = VecDeque::<(Pos, usize)>::new();
queue.push_back((start, 0));
while !queue.is_empty() {
let (pos, cur_len) = queue.pop_front().unwrap();
if !seen.contains(&pos) {
if pos == grid.dest {
return Some(cur_len);
}
seen.insert(pos);
for dir in &[Pos(1, 0), Pos(-1, 0), Pos(0, 1), Pos(0, -1)] {
let next = pos + *dir;
if grid.is_valid(next) && grid.get(next) <= grid.get(pos) + 1 {
queue.push_back((next, cur_len + 1));
}
}
}
}
None
}
fn part1(input: &str) -> usize {
let grid = Grid::parse(input);
find_path(grid.start, &grid).unwrap()
}
fn part2(input: &str) -> usize {
let grid = Grid::parse(input);
let low_height = b'a';
grid.iter()
.filter(|pos| grid.get(*pos) == low_height)
.filter_map(|pos| find_path(pos, &grid))
.min()
.unwrap()
}
fn main() {
println!("Part 1: {}", part1(include_str!("input.txt")));
println!("Part 2: {}", part2(include_str!("input.txt")));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test.txt")), 31);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test.txt")), 29);
}