-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.rs
More file actions
170 lines (147 loc) · 4.74 KB
/
17.rs
File metadata and controls
170 lines (147 loc) · 4.74 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
advent_of_code::solution!(17);
use std::collections::BinaryHeap;
use std::cmp::Ordering;
const UP: usize = 0;
const DOWN: usize = 1;
const LEFT: usize = 2;
const RIGHT: usize = 3;
#[derive(Copy, Clone, Eq, PartialEq)]
struct State {
cost: u16,
position: usize,
direction: usize,
}
impl Ord for State {
fn cmp(&self, other: &Self) -> Ordering {
other.cost.cmp(&self.cost)
}
}
impl PartialOrd for State {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
struct Maze {
data: Vec<u16>,
xlen: usize,
ylen: usize,
}
impl Maze {
pub fn new(input: &str) -> Self {
let mut data = Vec::new();
let mut ylen: usize = 0;
let mut xlen: usize = 0;
for line in input.lines() {
for char in line.chars() {
data.push(char.to_string().parse::<u16>().unwrap_or(u16::MAX));
}
xlen += 1;
if ylen == 0 {
ylen = line.len();
}
}
Self {
data,
xlen,
ylen,
}
}
pub fn deplace(&self, direction: usize, position: usize) -> Option<usize> {
let (x, y) = (position / self.ylen, position % self.ylen);
match direction {
UP => if x > 0 { Some(position - self.ylen) } else { None },
DOWN => if x < self.xlen - 1 { Some(position + self.ylen) } else { None },
LEFT => if y > 0 { Some(position - 1) } else { None },
RIGHT => if y < self.ylen - 1 { Some(position + 1) } else { None },
_ => panic!("Invalid direction"),
}
}
}
fn get_new_direction(direction: usize) -> [usize; 2] {
match direction {
UP | DOWN => [LEFT, RIGHT],
LEFT | RIGHT => [UP, DOWN],
_ => panic!("Invalid direction"),
}
}
pub fn find_path(input: &str, pas_min: usize, pas_max: usize) -> Option<u16> {
let grid = Maze::new(input);
let ecart = pas_max - pas_min;
let mut dist = [
vec![vec![u16::MAX; grid.data.len() * 3 * 4]; ecart],
vec![vec![u16::MAX; grid.data.len() * 3 * 4]; ecart],
vec![vec![u16::MAX; grid.data.len() * 3 * 4]; ecart],
vec![vec![u16::MAX; grid.data.len() * 3 * 4]; ecart]
];
let mut heap = BinaryHeap::new();
heap.push(State { cost: 0, position: 0, direction: RIGHT });
heap.push(State { cost: 0, position: 0, direction: DOWN });
'prochain: while let Some(state) = heap.pop() {
let mut next_position = state.position;
let mut next_cost = state.cost;
for _ in 0..pas_min {
next_position = match grid.deplace(state.direction, next_position) {
Some(pos) => pos,
None => continue 'prochain,
};
next_cost = next_cost + grid.data[next_position];
}
for i in 1..(1 + ecart) {
next_position = match grid.deplace(state.direction, next_position) {
Some(pos) => pos,
None => continue 'prochain,
};
next_cost = next_cost + grid.data[next_position];
if next_cost < dist[state.direction][i - 1][next_position] {
for new_direction in get_new_direction(state.direction) {
heap.push(State { cost: next_cost, position: next_position, direction: new_direction });
}
dist[state.direction][i - 1][next_position] = next_cost;
}
}
}
let j = grid.data.len() - 1;
let mut minval = u16::MAX;
for i in 0..ecart {
for k in 0..4 {
minval = minval.min(dist[k][i][j]);
}
}
Some(minval)
}
pub fn part_one(input: &str) -> Option<u16> {
find_path(input, 0, 3)
}
pub fn part_two(input: &str) -> Option<u16> {
find_path(input, 3, 10)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_maze() {
let input = advent_of_code::template::read_file("examples", DAY);
let grid = Maze::new(&input);
let mut pos = 0_usize;
assert_eq!(grid.data[pos], 2);
pos = grid.deplace(DOWN, pos).unwrap();
assert_eq!(grid.data[pos], 3);
pos = grid.deplace(RIGHT, pos).unwrap();
assert_eq!(grid.data[pos], 2);
pos = grid.deplace(UP, pos).unwrap();
assert_eq!(grid.data[pos], 4);
pos = grid.deplace(LEFT, pos).unwrap();
assert_eq!(grid.data[pos], 2);
assert_eq!(pos, 0);
}
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(102));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(94));
}
}