-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
126 lines (111 loc) Β· 2.88 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
124
125
126
#![allow(non_snake_case)]
use aoc::geometry::{Direction, Turn};
use aoc::graph::AStar;
use aoc::grid::{Cell, Grid, IntGrid};
use aoc::Puzzle;
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
struct Move {
cell: Cell,
dir: Option<Direction>,
cost: usize,
}
struct AoC2023_17;
impl AoC2023_17 {
fn solve(grid: &IntGrid, min_moves: usize, max_moves: usize) -> u32 {
let adjacent: &dyn Fn(Move) -> Vec<Move> = &|r#move: Move| {
let mut moves = vec![];
for dir in Direction::capital() {
if r#move.dir.is_some()
&& (r#move.dir.unwrap() == dir
|| r#move.dir.unwrap() == dir.turn(Turn::Around))
{
continue;
}
let mut cell = r#move.cell;
let mut hl: usize = 0;
for i in 1..=max_moves {
let o_cell = cell.try_at(dir);
if o_cell.is_none() || !grid.in_bounds(&o_cell.unwrap()) {
break;
}
cell = o_cell.unwrap();
hl += grid.get(&cell) as usize;
if i >= min_moves {
moves.push(Move {
cell,
dir: Some(dir),
cost: hl,
});
}
}
}
moves
};
let end: Cell = Cell::at(grid.height() - 1, grid.width() - 1);
AStar::distance(
Move {
cell: Cell::at(0, 0),
dir: None,
cost: 0,
},
|r#move| r#move.cell == end,
adjacent,
|_, r#move| r#move.cost,
) as u32
}
}
impl aoc::Puzzle for AoC2023_17 {
type Input = IntGrid;
type Output1 = u32;
type Output2 = u32;
aoc::puzzle_year_day!(2023, 17);
fn parse_input(&self, lines: Vec<String>) -> IntGrid {
IntGrid::from(&lines.iter().map(AsRef::as_ref).collect::<Vec<_>>())
}
fn part_1(&self, grid: &IntGrid) -> u32 {
AoC2023_17::solve(grid, 1, 3)
}
fn part_2(&self, grid: &IntGrid) -> u32 {
AoC2023_17::solve(grid, 4, 10)
}
fn samples(&self) {
aoc::puzzle_samples! {
self, part_1, TEST1, 102,
self, part_2, TEST1, 94,
self, part_2, TEST2, 71
};
}
}
fn main() {
AoC2023_17 {}.run(std::env::args());
}
const TEST1: &str = "\
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533
";
const TEST2: &str = "\
111111111111
999999999991
999999999991
999999999991
999999999991
";
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn samples() {
AoC2023_17 {}.samples();
}
}