aboutsummaryrefslogtreecommitdiffstats
path: root/day11/solution.zig
diff options
context:
space:
mode:
Diffstat (limited to 'day11/solution.zig')
-rw-r--r--day11/solution.zig67
1 files changed, 60 insertions, 7 deletions
diff --git a/day11/solution.zig b/day11/solution.zig
index 059a120..e9ddfed 100644
--- a/day11/solution.zig
+++ b/day11/solution.zig
@@ -9,15 +9,68 @@ const fin = mem.trim(u8, @embedFile("./input.txt"), &std.ascii.whitespace);
9var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 9var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10const allocator = gpa.allocator(); 10const allocator = gpa.allocator();
11 11
12pub fn part1() void { 12const u64HashMap = HashMap(u64, void, u64ctx, 80);
13 // Part 1 goes here 13const u64ctx = struct {
14} 14 pub fn hash(_: @This(), k: u64) u64 {
15 return k;
16 }
17
18 pub fn eql(_: @This(), lhs: u64, rhs: u64) bool {
19 return lhs == rhs;
20 }
21};
22
23pub fn solve(galaxies: [][2]usize, popRows: u64HashMap, popCols: u64HashMap) !void {
24 var nEmpty: usize = 0;
25 var sDist: usize = 0;
26
27 for (galaxies[0..], 0..) |this, i| {
28 for (galaxies[i + 1 ..]) |other| {
29 const ai = @min(this[0], other[0]);
30 const aj = @min(this[1], other[1]);
31 const bi = @max(this[0], other[0]);
32 const bj = @max(this[1], other[1]);
33
34 sDist += bi - ai + bj - aj;
15 35
16pub fn part2() void { 36 for (ai..bi) |row| {
17 // Part 2 goes here 37 if (popRows.get(row) == null) {
38 nEmpty += 1;
39 }
40 }
41
42 for (aj..bj) |col| {
43 if (popCols.get(col) == null) {
44 nEmpty += 1;
45 }
46 }
47 }
48 }
49
50 const part1 = sDist + nEmpty;
51 const part2 = sDist + nEmpty * 999_999;
52
53 print("{d}\n", .{part1});
54 print("{d}\n", .{part2});
18} 55}
19 56
20pub fn main() !void { 57pub fn main() !void {
21 part1(); 58 var splitLines = mem.splitScalar(u8, fin, '\n');
22 part2(); 59 var galaxies = ArrayList([2]usize).init(allocator);
60 var popRows = u64HashMap.init(allocator);
61 var popCols = u64HashMap.init(allocator);
62 defer galaxies.deinit();
63
64 var i: usize = 0;
65 while (splitLines.next()) |row| : (i += 1) {
66 for (row, 0..) |c, j| {
67 if (c == '#') {
68 try popRows.put(i, {});
69 try popCols.put(j, {});
70 try galaxies.append(.{ i, j });
71 }
72 }
73 }
74
75 try solve(galaxies.items, popRows, popCols);
23} 76}