-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_15.rs
188 lines (156 loc) · 5.87 KB
/
day_15.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::fs;
use std::collections::HashSet;
#[derive(Clone, Copy, PartialEq, Hash, Eq)]
struct Node {
weight: u32, // individual
total_weight: u32, // total needed to get this node
coord: (usize, usize), // (X, y)
}
pub fn run() {
let input = fs::read_to_string("inputs/input_15.txt")
.expect("Failed to read file");
// split input into vector of vector of ints
// only needs 8 bits per int since each one is a single digit (in base 10)
let input: Vec<Vec<u32>> = input
.split("\n")
.map(|s| s.chars().map(|x| x.to_string().parse().unwrap()).collect::<Vec<u32>>())
.collect();
part_one(&input);
part_two(&input);
}
fn part_one(input: &Vec<Vec<u32>>) {
let mut map: Vec<Vec<Node>> = vec!();
for i in 0..input.len() {
map.push(vec!());
for j in 0..input[0].len() {
map[i].push(Node {
weight: input[i][j],
total_weight: u32::MAX,
coord: (j, i),
});
}
};
println!("Part 1: {}", shortest_path(map));
}
fn part_two(input: &Vec<Vec<u32>>) {
let mut start_tile: Vec<Vec<Node>> = vec!();
let mut map: Vec<Vec<Node>> = vec!();
for i in 0..input.len() {
start_tile.push(vec!());
map.push(vec!());
for j in 0..input[0].len() {
start_tile[i].push(Node {
weight: input[i][j],
total_weight: u32::MAX,
coord: (j, i),
});
map[i].push(start_tile[i][j]);
}
}
// complete rest of map
for i in 1..5usize {
// fill next tile to first row of tiles
for row in 0..start_tile.len() {
for col in 0..start_tile[row].len() {
let mut cell = start_tile[row][col];
cell.weight = if cell.weight + i as u32 <= 9 { cell.weight + i as u32 } else { (cell.weight + i as u32) - 9 };
cell.coord = (cell.coord.0 + (i * start_tile[0].len()), cell.coord.1);
map[row].push(cell);
}
}
for _row in 0..start_tile.len() {
map.push(vec!());
}
// fill ith row
for j in 0..5usize {
for row in 0..start_tile.len() {
for col in 0..start_tile[row].len() {
let mut cell = start_tile[row][col];
cell.weight = if cell.weight + (i + j) as u32 <= 9 { cell.weight + (i + j) as u32 } else { (cell.weight + (i + j) as u32) - 9 };
cell.coord = (cell.coord.0 + (j * start_tile[0].len()), cell.coord.1 + ((i as usize) * start_tile[0].len()));
map[row + ((i as usize) * start_tile.len())].push(cell);
}
}
}
}
println!("Part 2: {}", shortest_path(map));
}
fn shortest_path(mut map: Vec<Vec<Node>>) -> u32 {
map[0][0].weight = 0;
map[0][0].total_weight = 0;
let end_coord = map[map.len()-1][map[0].len()-1].coord;
let mut finished_nodes: HashSet<Node> = HashSet::new();
let mut queue: Vec<Node> = vec!(map[0][0]);
// let mut nodes_examined = 0;
loop {
let node: Node = queue[0];
finished_nodes.insert(node);
queue.remove(0);
// nodes_examined += 1;
if node.coord == end_coord {
// println!("{} nodes examined", nodes_examined);
return node.total_weight;
}
let (x, y) = node.coord;
if x > 0 && !finished_nodes.contains(&map[y][x-1]) {
let old_weight = map[y][x-1].total_weight;
let new_weight = node.total_weight + map[y][x-1].weight;
if new_weight < old_weight {
if let Some(old_index) = queue.iter().position(|e| e == &map[y][x-1]) {
queue.remove(old_index);
}
map[y][x-1].total_weight = new_weight;
queue = insert(queue, map[y][x-1]);
}
}
if x < end_coord.0 && !finished_nodes.contains(&map[y][x+1]) {
let old_weight = map[y][x+1].total_weight;
let new_weight = node.total_weight + map[y][x+1].weight;
if new_weight < old_weight {
if let Some(old_index) = queue.iter().position(|e| e == &map[y][x+1]) {
queue.remove(old_index);
}
map[y][x+1].total_weight = new_weight;
queue = insert(queue, map[y][x+1]);
}
}
if y > 0 && !finished_nodes.contains(&map[y-1][x]) {
let old_weight = map[y-1][x].total_weight;
let new_weight = node.total_weight + map[y-1][x].weight;
if new_weight < old_weight {
if let Some(old_index) = queue.iter().position(|e| e == &map[y-1][x]) {
queue.remove(old_index);
}
map[y-1][x].total_weight = new_weight;
queue = insert(queue, map[y-1][x]);
}
}
if y < end_coord.1 && !finished_nodes.contains(&map[y+1][x]) {
let old_weight = map[y+1][x].total_weight;
let new_weight = node.total_weight + map[y+1][x].weight;
if new_weight < old_weight {
if let Some(old_index) = queue.iter().position(|e| e == &map[y+1][x]) {
queue.remove(old_index);
}
map[y+1][x].total_weight = new_weight;
queue = insert(queue, map[y+1][x]);
}
}
}
}
// insert so that it remains sorted
fn insert(mut queue: Vec<Node>, value: Node) -> Vec<Node> {
let mut inserted = false;
let mut previous = 0;
for i in 0..queue.len() {
if previous < value.total_weight && queue[i].total_weight >= value.total_weight {
queue.insert(i, value);
inserted = true;
}
previous = queue[i].total_weight;
}
if !inserted {
queue.push(value);
}
queue
}