adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
1e3e6a67ed346ca58e2a001e4126bcd6ae8a86f2
parent
3c1ee255e328002f4bd0d739e1262a8bae525bb4
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-12-20 07:47
2025-12-12

Diffstat

A 2025/12/solution.zig 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2025/12/test.txt 33 +++++++++++++++++++++++++++++++++

2 files changed, 114 insertions, 0 deletions


diff --git a/2025/12/solution.zig b/2025/12/solution.zig

@@ -0,0 +1,81 @@
   -1     1 const std = @import("std");
   -1     2 
   -1     3 fn check(blocks: [6]u9, width: usize, height: usize, counts: [6]usize) bool {
   -1     4     var totalBlocks: usize = 0;
   -1     5     for (counts) |c| {
   -1     6         totalBlocks += c;
   -1     7     }
   -1     8 
   -1     9     if ((width / 3) * (height / 3) >= totalBlocks) {
   -1    10         return true;
   -1    11     }
   -1    12 
   -1    13     var totalUnits: usize = 0;
   -1    14     for (counts, 0..) |c, i| {
   -1    15         totalUnits += c * @popCount(blocks[i]);
   -1    16     }
   -1    17     if (width * height < totalUnits) {
   -1    18         return false;
   -1    19     }
   -1    20 
   -1    21     // more complex cases don't actually exist in the input data
   -1    22     unreachable;
   -1    23 }
   -1    24 
   -1    25 pub fn main() !void {
   -1    26     const path: [:0]const u8 = std.mem.span(std.os.argv[1]);
   -1    27     var file = try std.fs.cwd().openFile(path, .{});
   -1    28     defer file.close();
   -1    29 
   -1    30     var buffer: [1024]u8 = undefined;
   -1    31     var reader = file.reader(&buffer);
   -1    32 
   -1    33     var i: usize = 0;
   -1    34     var blocks = [_]u9{0} ** 6;
   -1    35 
   -1    36     var part1: usize = 0;
   -1    37 
   -1    38     while (reader.interface.peekDelimiterExclusive('\n')) |line| {
   -1    39         reader.interface.toss(line.len + 1);
   -1    40 
   -1    41         const j = i / 5;
   -1    42 
   -1    43         if (j < 6) {
   -1    44             for (line) |c| {
   -1    45                 if (c == '#') {
   -1    46                     blocks[j] <<= 1;
   -1    47                     blocks[j] |= 1;
   -1    48                 } else if (c == '.') {
   -1    49                     blocks[j] <<= 1;
   -1    50                 }
   -1    51             }
   -1    52         } else {
   -1    53             var x1: usize = 0;
   -1    54             var x2: usize = std.mem.indexOfScalar(u8, line, 'x').?;
   -1    55             const width = try std.fmt.parseInt(usize, line[x1..x2], 10);
   -1    56 
   -1    57             x1 = x2 + 1;
   -1    58             x2 = std.mem.indexOfScalar(u8, line, ':').?;
   -1    59             const height = try std.fmt.parseInt(usize, line[x1..x2], 10);
   -1    60 
   -1    61             var counts = [_]usize{0} ** 6;
   -1    62             x2 += 1;
   -1    63             for (0..6) |k| {
   -1    64                 x1 = x2 + 1;
   -1    65                 x2 = x1 + (std.mem.indexOfScalar(u8, line[x1..], ' ') orelse (line.len - x1));
   -1    66                 counts[k] = try std.fmt.parseInt(usize, line[x1..x2], 10);
   -1    67             }
   -1    68 
   -1    69             if (check(blocks, width, height, counts)) {
   -1    70                 part1 += 1;
   -1    71             }
   -1    72         }
   -1    73 
   -1    74         i += 1;
   -1    75     } else |err| switch (err) {
   -1    76         error.EndOfStream => {},
   -1    77         else => |e| return e,
   -1    78     }
   -1    79 
   -1    80     std.debug.print("part1: {}\n", .{part1});
   -1    81 }

diff --git a/2025/12/test.txt b/2025/12/test.txt

@@ -0,0 +1,33 @@
   -1     1 0:
   -1     2 ###
   -1     3 ##.
   -1     4 ##.
   -1     5 
   -1     6 1:
   -1     7 ###
   -1     8 ##.
   -1     9 .##
   -1    10 
   -1    11 2:
   -1    12 .##
   -1    13 ###
   -1    14 ##.
   -1    15 
   -1    16 3:
   -1    17 ##.
   -1    18 ###
   -1    19 ##.
   -1    20 
   -1    21 4:
   -1    22 ###
   -1    23 #..
   -1    24 ###
   -1    25 
   -1    26 5:
   -1    27 ###
   -1    28 .#.
   -1    29 ###
   -1    30 
   -1    31 4x4: 0 0 0 0 2 0
   -1    32 12x5: 1 0 1 0 2 2
   -1    33 12x5: 1 0 1 0 3 2