-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.zig
68 lines (56 loc) · 2.3 KB
/
day05.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
const std = @import("std");
const input = @embedFile("input05.txt");
const Rules = std.hash_map.AutoHashMapUnmanaged(u8, std.ArrayListUnmanaged(u8));
fn bad_pages(rules: *const Rules, pages: []u8) bool {
for (0..pages.len - 1) |b| {
const before = pages[b];
for (b + 1..pages.len) |a| {
const after = pages[a];
if (rules.get(after)) |a_after_nums| {
if (std.mem.indexOfScalar(u8, a_after_nums.items, before)) |_| {
return true;
}
}
}
}
return false;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
const alloc = gpa.allocator();
defer std.debug.assert(gpa.deinit() == .ok);
var rules = Rules.empty;
defer {
var rules_iter = rules.valueIterator();
while (rules_iter.next()) |after_nums| after_nums.deinit(alloc);
rules.deinit(alloc);
}
var lines = std.mem.splitScalar(u8, input, '\n');
// Read rule lines into a hash map.
while (lines.next()) |line| {
if (line.len == 0) break;
var rule_nums = std.mem.splitScalar(u8, line, '|');
const before_num_str = rule_nums.next() orelse return error.MissingRuleBeforeNumber;
const after_num_str = rule_nums.next() orelse return error.MissingRuleAfterNumber;
const before_num = try std.fmt.parseInt(u8, before_num_str, 10);
const after_num = try std.fmt.parseInt(u8, after_num_str, 10);
const gop_res = try rules.getOrPut(alloc, before_num);
if (!gop_res.found_existing) gop_res.value_ptr.* = std.ArrayListUnmanaged(u8).empty;
const after_list = gop_res.value_ptr;
try after_list.append(alloc, after_num);
}
// Process page update list lines.
var total: usize = 0;
while (lines.next()) |line| {
if (line.len == 0) break;
var pages = std.ArrayListUnmanaged(u8).empty;
defer pages.deinit(alloc);
var page_nums = std.mem.splitScalar(u8, line, ',');
while (page_nums.next()) |page_num_str| {
const page_num = try std.fmt.parseInt(u8, page_num_str, 10);
try pages.append(alloc, page_num);
}
if (!bad_pages(&rules, pages.items)) total += pages.items[pages.items.len / 2];
}
std.debug.print("{d}\n", .{ total });
}