-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.zig
87 lines (72 loc) · 2.85 KB
/
day8.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
const std = @import("std");
const util = @import("util.zig");
const Coord = @import("coord.zig").Coord;
pub const std_options: std.Options = .{ .log_level = .info };
const Day8Error = error{
NotEnoughArgs,
InvalidPart,
};
pub fn main() !void {
const argv = try std.process.argsAlloc(std.heap.page_allocator);
defer std.process.argsFree(std.heap.page_allocator, argv);
if (argv.len != 3) {
std.log.err("3 args required but only {d} provided", .{argv.len});
return Day8Error.NotEnoughArgs;
}
const file_path = argv[1];
const part = try std.fmt.parseInt(i32, argv[2], 10);
_ = part;
const file = try std.fs.cwd().openFile(file_path, .{});
// Store a set of coordinates per-frequency
var freq_coords = std.AutoHashMap(u8, std.ArrayList(Coord)).init(std.heap.page_allocator);
defer freq_coords.deinit();
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var buf: [1024]u8 = undefined;
var h: i32 = 0;
var w: i32 = 0;
while (try util.readLineOrEof(file.reader(), &buf)) |line| : (h += 1) {
std.log.debug("Input line: {s}", .{line});
for (line, 0..) |c, _x| {
switch (c) {
'a'...'z', 'A'...'Z', '0'...'9' => {
const x: i32 = @intCast(_x);
const coord = Coord{ .x = x, .y = h };
if (freq_coords.getPtr(c)) |coords| {
try coords.append(coord);
} else {
var new_list = std.ArrayList(Coord).init(allocator);
try new_list.append(coord);
try freq_coords.put(c, new_list);
}
},
else => {},
}
}
w = @intCast(line.len);
}
const bounds = Coord{ .x = w, .y = h };
var it = freq_coords.iterator();
var antinodes =
std.AutoHashMap(Coord, void).init(std.heap.page_allocator);
defer antinodes.deinit();
while (it.next()) |entry| {
std.log.debug("Checks for freq '{c}'", .{entry.key_ptr.*});
const items = entry.value_ptr.items;
// For every unique pairing of coordinates with the same frequency
for (items[0..(items.len - 1)], 0..) |a, i| {
for (items[(i + 1)..]) |b| {
// Compute both the forward and reverse signal resonance
const ab = a.antinode(b);
const ba = b.antinode(a);
std.log.debug(" {} <=> {}: {} and {}", .{ a, b, ab, ba });
if (ab.inbounds(bounds))
try antinodes.put(ab, {});
if (ba.inbounds(bounds))
try antinodes.put(ba, {});
}
}
}
std.log.info("Total unique antinodes: {d}", .{antinodes.count()});
}