-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.zig
241 lines (199 loc) · 6.73 KB
/
day15.zig
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const std = @import("std");
const print = std.debug.print;
const assert = std.debug.assert;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;
const math = std.math;
const absInt = std.math.absInt;
const indexOfScalar = std.mem.indexOfScalar;
const rotate = std.mem.rotate;
const round = std.math.round;
const sort = std.sort.sort;
const ArrayList = std.ArrayList;
const BoundedArray = std.BoundedArray;
const alloc = std.heap.page_allocator;
const Parser = @import("lib/parse3.zig").Parser;
const REAL_INPUT = @embedFile("inputs/day15.txt");
pub fn main() !void {
print("Part 1: {}\n", .{try part1(&Parser.init(REAL_INPUT))});
print("Part 2: {}\n", .{try part2(&Parser.init(REAL_INPUT))});
}
const Node = struct {
visited: bool = false,
cost: u8,
prev_node_idx: usize = math.maxInt(usize),
min_cost_to_here: u64 = math.maxInt(u64),
};
const MAX_RISK = Node{
.visited = true,
.cost = math.maxInt(u8),
};
fn part1(input: *Parser) !u64 {
const MAX_DIM = 102;
var world_raw = try BoundedArray(Node, MAX_DIM * MAX_DIM).init(0);
var stride: usize = undefined;
var width: usize = undefined;
var height: usize = 0;
while (input.subparse("\n")) |*line| {
if (height == 0) {
width = line.source.len;
stride = width + 2;
try world_raw.appendNTimes(MAX_RISK, stride);
} else {
assert(width == line.source.len);
}
try world_raw.append(MAX_RISK);
while (try line.takeTypeByCount(u8, 1)) |risk| {
const node = Node{
.cost = risk,
};
try world_raw.append(node);
}
try world_raw.append(MAX_RISK);
height += 1;
}
try world_raw.appendNTimes(MAX_RISK, stride);
var world = world_raw.slice();
const start = stride + 1;
const end = start + (height - 1) * stride + width - 1;
world[start].min_cost_to_here = 0;
world[start].prev_node_idx = 0;
var fringe = try BoundedArray(usize, MAX_DIM * MAX_DIM).init(0);
try fringe.append(start);
while (world[end].prev_node_idx == MAX_RISK.prev_node_idx) {
var lowest_unvisited_cost: u64 = math.maxInt(u64);
var index_in_fringe: usize = undefined;
for (fringe.slice()) |f_idx, i| {
const f_cost = world[f_idx].min_cost_to_here;
if (f_cost < lowest_unvisited_cost) {
index_in_fringe = i;
lowest_unvisited_cost = f_cost;
}
}
const idx = fringe.swapRemove(index_in_fringe);
const node = &world[idx];
node.visited = true;
// Node to the right
const neighbour_indicies = [_]usize {
idx + 1, idx - 1, idx + stride, idx - stride
};
for (neighbour_indicies) |n_idx| {
const n_node = &world[n_idx];
const cost = node.min_cost_to_here + n_node.cost;
if (cost < n_node.min_cost_to_here) {
n_node.min_cost_to_here = cost;
n_node.prev_node_idx = idx;
}
if (
!n_node.visited and
mem.indexOfScalar(usize, fringe.slice(), n_idx) == null
) {
try fringe.append(n_idx);
}
}
}
return world[end].min_cost_to_here;
}
fn part2(input: *Parser) !u64 {
var parsed_input = try BoundedArray(u8, 100 * 100).init(0);
var parse_width: usize = mem.indexOfScalar(u8, input.source, '\n').?;
for (input.source) |c| {
if (c == '\n') continue;
try parsed_input.append(c - '0');
}
const MAX_DIM = 502;
var world_raw = try ArrayList(Node).initCapacity(alloc, MAX_DIM * MAX_DIM);
defer world_raw.deinit();
var width: usize = parse_width * 5;
var stride: usize = width + 2;
var start = stride + 1;
try world_raw.appendNTimes(MAX_RISK, stride);
var repeat_y: u8 = 0;
while (repeat_y < 5) : (repeat_y += 1) {
var idx: usize = 0;
while (idx < parsed_input.len) : (idx += parse_width) {
try world_raw.append(MAX_RISK);
const row = parsed_input.slice()[idx..idx + parse_width];
var repeat_x: u8 = 0;
while (repeat_x < 5) : (repeat_x += 1) {
for (row) |risk| {
var node = .{ .cost = risk + repeat_x + repeat_y };
while (node.cost > 9) { node.cost -= 9; }
try world_raw.append(node);
}
}
try world_raw.append(MAX_RISK);
}
}
try world_raw.appendNTimes(MAX_RISK, stride);
var world = world_raw.toOwnedSlice();
defer alloc.free(world);
const end = world.len - stride - 2;
world[start].min_cost_to_here = 0;
world[start].prev_node_idx = 0;
var fringe = try BoundedArray(usize, MAX_DIM * MAX_DIM).init(0);
try fringe.append(start);
while (world[end].prev_node_idx == MAX_RISK.prev_node_idx) {
var lowest_unvisited_cost: u64 = math.maxInt(u64);
var index_in_fringe: usize = undefined;
for (fringe.slice()) |f_idx, i| {
const f_cost = world[f_idx].min_cost_to_here;
if (f_cost < lowest_unvisited_cost) {
index_in_fringe = i;
lowest_unvisited_cost = f_cost;
}
}
const idx = fringe.swapRemove(index_in_fringe);
const node = &world[idx];
node.visited = true;
// Node to the right
const neighbour_indicies = [_]usize {
idx + 1, idx - 1, idx + stride, idx - stride
};
for (neighbour_indicies) |n_idx| {
const n_node = &world[n_idx];
const cost = node.min_cost_to_here + n_node.cost;
if (cost < n_node.min_cost_to_here) {
n_node.min_cost_to_here = cost;
n_node.prev_node_idx = idx;
}
if (
!n_node.visited and
mem.indexOfScalar(usize, fringe.slice(), n_idx) == null
) {
try fringe.append(n_idx);
}
}
}
return world[end].min_cost_to_here;
}
test "Part 1" {
const test_input =
\\1163751742
\\1381373672
\\2136511328
\\3694931569
\\7463417111
\\1319128137
\\1359912421
\\3125421639
\\1293138521
\\2311944581
;
try expectEqual(@as(u64, 40), try part1(&Parser.init(test_input)));
}
test "Part 2" {
const test_input =
\\1163751742
\\1381373672
\\2136511328
\\3694931569
\\7463417111
\\1319128137
\\1359912421
\\3125421639
\\1293138521
\\2311944581
;
try expectEqual(@as(u64, 315), try part2(&Parser.init(test_input)));
}