From c73fa4177dc2186fd915a54fd26c3d28286190a4 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Tue, 12 Dec 2023 00:58:48 +0200 Subject: day11 --- day11/example.txt | 10 ++++++++ day11/solution.zig | 67 ++++++++++++++++++++++++++++++++++++++++++++++++------ template.zig | 11 +++------ 3 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 day11/example.txt diff --git a/day11/example.txt b/day11/example.txt new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/day11/example.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... 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); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); -pub fn part1() void { - // Part 1 goes here -} +const u64HashMap = HashMap(u64, void, u64ctx, 80); +const u64ctx = struct { + pub fn hash(_: @This(), k: u64) u64 { + return k; + } + + pub fn eql(_: @This(), lhs: u64, rhs: u64) bool { + return lhs == rhs; + } +}; + +pub fn solve(galaxies: [][2]usize, popRows: u64HashMap, popCols: u64HashMap) !void { + var nEmpty: usize = 0; + var sDist: usize = 0; + + for (galaxies[0..], 0..) |this, i| { + for (galaxies[i + 1 ..]) |other| { + const ai = @min(this[0], other[0]); + const aj = @min(this[1], other[1]); + const bi = @max(this[0], other[0]); + const bj = @max(this[1], other[1]); + + sDist += bi - ai + bj - aj; -pub fn part2() void { - // Part 2 goes here + for (ai..bi) |row| { + if (popRows.get(row) == null) { + nEmpty += 1; + } + } + + for (aj..bj) |col| { + if (popCols.get(col) == null) { + nEmpty += 1; + } + } + } + } + + const part1 = sDist + nEmpty; + const part2 = sDist + nEmpty * 999_999; + + print("{d}\n", .{part1}); + print("{d}\n", .{part2}); } pub fn main() !void { - part1(); - part2(); + var splitLines = mem.splitScalar(u8, fin, '\n'); + var galaxies = ArrayList([2]usize).init(allocator); + var popRows = u64HashMap.init(allocator); + var popCols = u64HashMap.init(allocator); + defer galaxies.deinit(); + + var i: usize = 0; + while (splitLines.next()) |row| : (i += 1) { + for (row, 0..) |c, j| { + if (c == '#') { + try popRows.put(i, {}); + try popCols.put(j, {}); + try galaxies.append(.{ i, j }); + } + } + } + + try solve(galaxies.items, popRows, popCols); } diff --git a/template.zig b/template.zig index 059a120..c9ec529 100644 --- a/template.zig +++ b/template.zig @@ -9,15 +9,10 @@ const fin = mem.trim(u8, @embedFile("./input.txt"), &std.ascii.whitespace); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); -pub fn part1() void { - // Part 1 goes here -} - -pub fn part2() void { - // Part 2 goes here +pub fn solve() void { + // solve } pub fn main() !void { - part1(); - part2(); + solve(); } -- cgit v1.2.3