-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.zig
249 lines (211 loc) · 6.19 KB
/
day12.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
242
243
244
245
246
247
248
249
const std = @import("std");
const print = std.debug.print;
const assert = std.debug.assert;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;
const absInt = std.math.absInt;
const indexOfScalar = std.mem.indexOfScalar;
const max = std.math.max;
const min = std.math.min;
const rotate = std.mem.rotate;
const round = std.math.round;
const sort = std.sort.sort;
const BoundedArray = std.BoundedArray;
const Parser = @import("lib/parse3.zig").Parser;
const REAL_INPUT = @embedFile("inputs/day12.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 Cave = struct {
id: []const u8,
is_big: bool,
neighbours: BoundedArray(*Cave, 25),
};
fn findOrCreateCave(caves: *BoundedArray(Cave, 50), id: []const u8) !*Cave {
for (caves.slice()) |*c| {
if (mem.eql(u8, c.id, id)) {
return c;
}
} else {
var new_cave = try caves.addOne();
new_cave.* = Cave{
.id = id,
.is_big = std.ascii.isUpper(id[0]),
.neighbours = try BoundedArray(*Cave, 25).init(0),
};
return new_cave;
}
}
fn part1(input: *Parser) !u64 {
var caves = try BoundedArray(Cave, 50).init(0);
while (input.subparse("\n")) |*line| {
const start = (try line.takeType([]const u8, "-")).?;
const end = (try line.takeType([]const u8, "-")).?;
var start_cave = try findOrCreateCave(&caves, start);
var end_cave = try findOrCreateCave(&caves, end);
try start_cave.neighbours.append(end_cave);
try end_cave.neighbours.append(start_cave);
}
var already_seen = try BoundedArray(*const Cave, 50).init(0);
for (caves.slice()) |*cave| {
if (mem.eql(u8, cave.id, "start")) {
try already_seen.append(cave);
break;
}
}
assert(already_seen.len == 1);
return try traverse_and_count(already_seen.get(0), &already_seen);
}
fn traverse_and_count(here: *const Cave, already_visited: *BoundedArray(*const Cave, 50)) !u64 {
if (mem.eql(u8, here.id, "end")) return 1;
var paths_through_here: u64 = 0;
for (here.neighbours.constSlice()) |neighbour| {
if (!neighbour.is_big) {
const index = mem.indexOfScalar(*const Cave, already_visited.slice(), neighbour);
if (index) |_| continue;
}
try already_visited.append(neighbour);
defer _ = already_visited.pop();
paths_through_here += traverse_and_count(neighbour, already_visited) catch unreachable;
}
return paths_through_here;
}
fn part2(input: *Parser) !u64 {
var caves = try BoundedArray(Cave, 50).init(0);
while (input.subparse("\n")) |*line| {
const start = (try line.takeType([]const u8, "-")).?;
const end = (try line.takeType([]const u8, "-")).?;
var start_cave = try findOrCreateCave(&caves, start);
var end_cave = try findOrCreateCave(&caves, end);
if (!mem.eql(u8, end_cave.id, "start")) try start_cave.neighbours.append(end_cave);
if (!mem.eql(u8, start_cave.id, "start")) try end_cave.neighbours.append(start_cave);
}
var already_seen = try BoundedArray(*const Cave, 200).init(0);
for (caves.slice()) |*cave| {
if (mem.eql(u8, cave.id, "start")) {
try already_seen.append(cave);
break;
}
}
assert(already_seen.len == 1);
return try traverse_and_count_with_repeat(already_seen.get(0), &already_seen, false);
}
fn traverse_and_count_with_repeat(here: *const Cave, already_visited: *BoundedArray(*const Cave, 200), repeat_used: bool) !u64 {
if (mem.eql(u8, here.id, "end")) return 1;
var paths_through_here: u64 = 0;
for (here.neighbours.constSlice()) |neighbour| {
var is_repeat = repeat_used;
if (!neighbour.is_big) {
const index = mem.indexOfScalar(*const Cave, already_visited.slice(), neighbour);
if (index) |_| {
if (repeat_used) continue;
is_repeat = true;
}
}
try already_visited.append(neighbour);
defer _ = already_visited.pop();
paths_through_here += traverse_and_count_with_repeat(neighbour, already_visited, is_repeat) catch unreachable;
}
return paths_through_here;
}
test "Part 1 small" {
const test_input =
\\start-A
\\start-b
\\A-c
\\A-b
\\b-d
\\A-end
\\b-end
;
try expectEqual(@as(u64, 10), try part1(&Parser.init(test_input)));
}
test "Part 1 medium" {
const test_input =
\\dc-end
\\HN-start
\\start-kj
\\dc-start
\\dc-HN
\\LN-dc
\\HN-end
\\kj-sa
\\kj-HN
\\kj-dc
;
try expectEqual(@as(u64, 19), try part1(&Parser.init(test_input)));
}
test "Part 1 large" {
const test_input =
\\fs-end
\\he-DX
\\fs-he
\\start-DX
\\pj-DX
\\end-zg
\\zg-sl
\\zg-pj
\\pj-he
\\RW-he
\\fs-DX
\\pj-RW
\\zg-RW
\\start-pj
\\he-WI
\\zg-he
\\pj-fs
\\start-RW
;
try expectEqual(@as(u64, 226), try part1(&Parser.init(test_input)));
}
test "Part 2 small" {
const test_input =
\\start-A
\\start-b
\\A-c
\\A-b
\\b-d
\\A-end
\\b-end
;
try expectEqual(@as(u64, 36), try part2(&Parser.init(test_input)));
}
test "Part 2 medium" {
const test_input =
\\dc-end
\\HN-start
\\start-kj
\\dc-start
\\dc-HN
\\LN-dc
\\HN-end
\\kj-sa
\\kj-HN
\\kj-dc
;
try expectEqual(@as(u64, 103), try part2(&Parser.init(test_input)));
}
test "Part 2 large" {
const test_input =
\\fs-end
\\he-DX
\\fs-he
\\start-DX
\\pj-DX
\\end-zg
\\zg-sl
\\zg-pj
\\pj-he
\\RW-he
\\fs-DX
\\pj-RW
\\zg-RW
\\start-pj
\\he-WI
\\zg-he
\\pj-fs
\\start-RW
;
try expectEqual(@as(u64, 3509), try part2(&Parser.init(test_input)));
}