aoc2023

Advent of Code 2023
Log | Files | Refs | README

day11.zig (3075B)


      1 const std = @import("std");
      2 
      3 pub fn main() !void {
      4     try std.fmt.format(std.io.getStdOut().writer(), "day 11 pt1: {}\n", .{try solve_pt1(std.heap.page_allocator, puzzle_input)});
      5     try std.fmt.format(std.io.getStdOut().writer(), "day 11 pt2: {}\n", .{try solve_pt2(std.heap.page_allocator, puzzle_input)});
      6 }
      7 
      8 const Coord = struct {
      9     x: usize,
     10     y: usize,
     11 };
     12 
     13 inline fn get(grid: []const u8, width: usize, x: usize, y: usize) u8 {
     14     return grid[y*(width+1) + x];
     15 }
     16 
     17 fn solve_pt1(a: std.mem.Allocator, input: []const u8) !u64 {
     18     return solve(a, input, 2);
     19 }
     20 fn solve_pt2(a: std.mem.Allocator, input: []const u8) !u64 {
     21     return solve(a, input, 1000000);
     22 }
     23 fn solve(a: std.mem.Allocator, input: []const u8, multiplier: usize) !u64 {
     24     const width: usize = std.mem.indexOfScalar(u8, input, '\n') orelse return error.BadInput;
     25     const height: usize = (input.len / width + 1) - 1;
     26     // std.log.warn("width {} height {}", .{width, height});
     27     const x_adjust = try a.alloc(usize, width);
     28     defer a.free(x_adjust);
     29     const y_adjust = try a.alloc(usize, height);
     30     defer a.free(y_adjust);
     31     var galaxies = std.ArrayList(Coord).init(a);
     32     defer galaxies.deinit();
     33     var x_adj: usize = 0;
     34     for (0..width) |x| {
     35         var allspace = true;
     36         for (0..height) |y| {
     37             const ch = get(input, width, x, y);
     38             if (ch == '#') {
     39                 try galaxies.append(.{.x = x, .y = y});
     40                 allspace = false;
     41             }
     42         }
     43         if (allspace) {
     44             x_adj += (multiplier-1);
     45         }
     46         x_adjust[x] = x_adj;
     47     }
     48     var y_adj:usize = 0;
     49     for (0..height) |y| {
     50         var allspace = true;
     51         for (0..width) |x| {
     52             const ch = get(input, width, x, y);
     53             if (ch == '#') {
     54                 allspace = false;
     55             }
     56         }
     57         if (allspace) {
     58             y_adj += (multiplier-1);
     59         }
     60         y_adjust[y] = y_adj;
     61     }
     62 
     63     for (galaxies.items) |*gal| {
     64         const x = gal.x;
     65         const y = gal.y;
     66         gal.* = .{
     67             .x = x + x_adjust[x],
     68             .y = y + y_adjust[y],
     69         };
     70     }
     71 
     72     var sum: usize = 0;
     73     for (0..galaxies.items.len) |ix1| {
     74         for (ix1..galaxies.items.len) |ix2| {
     75             const g1 = galaxies.items[ix1];
     76             const g2 = galaxies.items[ix2];
     77             const x_diff = @max(g1.x, g2.x) - @min(g1.x, g2.x);
     78             const y_diff = @max(g1.y, g2.y) - @min(g1.y, g2.y);
     79             sum += x_diff + y_diff;
     80         }
     81     }
     82 
     83     return sum;
     84 }
     85 
     86 test "pt1" {
     87     try std.testing.expectEqual(@as(u64, 374), try solve_pt1(std.testing.allocator, test_input));
     88 }
     89 test "pt2" {
     90     try std.testing.expectEqual(@as(u64, 1030), try solve(std.testing.allocator, test_input, 10));
     91 }
     92 test "pt2_2" {
     93     try std.testing.expectEqual(@as(u64, 8410), try solve(std.testing.allocator, test_input, 100));
     94 }
     95 
     96 const test_input = 
     97 \\...#......
     98 \\.......#..
     99 \\#.........
    100 \\..........
    101 \\......#...
    102 \\.#........
    103 \\.........#
    104 \\..........
    105 \\.......#..
    106 \\#...#.....
    107 ;
    108 
    109 const puzzle_input = @embedFile("day11.in");