-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
144 lines (132 loc) · 4.13 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::collections::HashMap;
use std::str::FromStr;
const FILE_PATH: &str = "input.txt";
type Coords = [usize; 2];
#[derive(Debug)]
struct Map {
map: Vec<Vec<u8>>,
max_coords: Coords,
}
impl FromStr for Map {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let map = s
.lines()
.map(|line| {
line.chars()
.map(|c| {
c.to_digit(10)
.and_then(|d| d.try_into().ok())
.ok_or(format!("Invalid line: {}", line))
})
.collect()
})
.collect::<Result<Vec<Vec<u8>>, Self::Err>>()?;
let y = map
.len()
.checked_sub(1)
.ok_or_else(|| String::from("Map is empty"))?;
let x = map
.last()
.and_then(|l| l.len().checked_sub(1))
.ok_or_else(|| String::from("Map is empty"))?;
Ok(Self {
max_coords: [x, y],
map,
})
}
}
impl Map {
fn neighbor_coordinates(&self, [x, y]: Coords) -> Vec<(Coords, u8)> {
[usize::checked_add, usize::checked_sub]
.iter()
.flat_map(|op| {
[(1, 0), (0, 1)]
.iter()
.filter_map(|(dx, dy)| {
let [x, y] = [op(x, *dx)?, op(y, *dy)?];
let cost = self.map.get(y)?.get(x)?;
Some(([x, y], *cost))
})
.collect::<Vec<(Coords, u8)>>()
})
.collect()
}
fn pop_from_stack(stack: &mut HashMap<Coords, usize>) -> Option<(Coords, usize)> {
let (coord, cost) = stack
.iter()
.min_by_key(|(_c, v)| *v)
.map(|(c, v)| (*c, *v))?;
stack.remove(&coord);
Some((coord, cost))
}
fn find_cheapest_path(&self) -> Option<usize> {
let mut stack = HashMap::new();
stack.insert([0, 0], 0);
let mut handled = vec![];
while let Some((coord, cost)) = Self::pop_from_stack(&mut stack) {
if coord == self.max_coords {
return Some(cost);
}
for (neighbor, new_cost) in self
.neighbor_coordinates(coord)
.into_iter()
.filter(|(c, _)| !handled.contains(c))
{
let new_cost = cost + new_cost as usize;
let entry = stack.entry(neighbor).or_insert(new_cost);
if *entry > new_cost {
*entry = new_cost;
}
}
handled.push(coord);
}
None
}
}
fn incremented_map(map: &[Vec<u8>], delta: u8) -> Vec<Vec<u8>> {
map.iter().map(|vec| incremented_vec(vec, delta)).collect()
}
fn incremented_vec(vec: &[u8], delta: u8) -> Vec<u8> {
vec.iter()
.map(|v| {
let value = v + delta;
if value > 9 {
1
} else {
value
}
})
.collect()
}
fn main() {
let map = Map::from_str(std::fs::read_to_string(FILE_PATH).unwrap().as_str()).unwrap();
println!("Part 1: {}", map.find_cheapest_path().unwrap());
let mut prev = map.map;
let new_map: Vec<Vec<u8>> = (0..5)
.map(|y| {
if y > 0 {
prev = incremented_map(&prev, 1);
}
prev.clone()
})
.flat_map(|map| {
map.iter()
.map(|vec| {
let mut prev_vec = vec.clone();
(1..5).fold(prev_vec.clone(), |mut acc, _x| {
let new_vec: Vec<u8> = incremented_vec(&prev_vec, 1);
acc.extend(new_vec.clone());
prev_vec = new_vec;
acc
})
})
.collect::<Vec<Vec<u8>>>()
})
.collect();
let map = Map {
max_coords: [new_map[0].len() - 1, new_map.len() - 1],
map: new_map,
};
println!("Part 2: {}", map.find_cheapest_path().unwrap());
}