diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2023-12-12 00:58:48 +0200 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-10-28 23:20:45 +0200 |
| commit | c73fa4177dc2186fd915a54fd26c3d28286190a4 (patch) | |
| tree | cacdeb5a0071c69acf99db2dd84eada8c74a4cd9 | |
| parent | day10 (diff) | |
| download | aoc23-c73fa4177dc2186fd915a54fd26c3d28286190a4.tar.gz aoc23-c73fa4177dc2186fd915a54fd26c3d28286190a4.zip | |
day11
| -rw-r--r-- | day11/example.txt | 10 | ||||
| -rw-r--r-- | day11/solution.zig | 67 | ||||
| -rw-r--r-- | template.zig | 11 |
3 files changed, 73 insertions, 15 deletions
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 @@ | |||
| 1 | ...#...... | ||
| 2 | .......#.. | ||
| 3 | #......... | ||
| 4 | .......... | ||
| 5 | ......#... | ||
| 6 | .#........ | ||
| 7 | .........# | ||
| 8 | .......... | ||
| 9 | .......#.. | ||
| 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); | |||
| 9 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | 9 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 10 | const allocator = gpa.allocator(); | 10 | const allocator = gpa.allocator(); |
| 11 | 11 | ||
| 12 | pub fn part1() void { | 12 | const u64HashMap = HashMap(u64, void, u64ctx, 80); |
| 13 | // Part 1 goes here | 13 | const 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 | |||
| 23 | pub 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 | ||
| 16 | pub 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 | ||
| 20 | pub fn main() !void { | 57 | pub 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 | } |
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); | |||
| 9 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | 9 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 10 | const allocator = gpa.allocator(); | 10 | const allocator = gpa.allocator(); |
| 11 | 11 | ||
| 12 | pub fn part1() void { | 12 | pub fn solve() void { |
| 13 | // Part 1 goes here | 13 | // solve |
| 14 | } | ||
| 15 | |||
| 16 | pub fn part2() void { | ||
| 17 | // Part 2 goes here | ||
| 18 | } | 14 | } |
| 19 | 15 | ||
| 20 | pub fn main() !void { | 16 | pub fn main() !void { |
| 21 | part1(); | 17 | solve(); |
| 22 | part2(); | ||
| 23 | } | 18 | } |
