/
main.zig
49 lines (37 loc) · 1.21 KB
/
main.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
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const map = std.hash_map;
const heap = std.heap;
const Vec = std.ArrayList;
const input = @embedFile("../input.txt");
fn solve(comptime N: type) !N {
var frequency = N(0);
var allocator = heap.DirectAllocator.init();
defer allocator.deinit();
var arena_alloc = heap.ArenaAllocator.init(&allocator.allocator);
defer arena_alloc.deinit();
var parsed = Vec(N).init(&arena_alloc.allocator);
defer parsed.deinit();
comptime var splitter = comptime mem.split(input, "\n");
inline while(comptime splitter.next()) |slice| {
const num = comptime try fmt.parseInt(N, slice, 10);
try parsed.append(num);
}
var set = map.AutoHashMap(N, void).init(&arena_alloc.allocator);
defer set.deinit();
while (true) {
for(parsed.toSliceConst()) |*num| {
frequency += num.*;
const entry = try set.getOrPut(frequency);
if (entry.found_existing) {
return entry.kv.key;
}
}
}
}
pub fn main() !void {
@setEvalBranchQuota(500000);
const answer = try solve(i32);
std.debug.warn("part 2: {}\n", answer);
}