adventofcode

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

commit
f58867f6b92fd1af57ef316b25f34976966e6d6d
parent
9945b0a73adbee949695bbbce968d3601d20e022
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-12-11 06:20
2025-12-09

Diffstat

A 2025/09/solution.zig 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2025/09/test.txt 8 ++++++++

2 files changed, 94 insertions, 0 deletions


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

@@ -0,0 +1,86 @@
   -1     1 const std = @import("std");
   -1     2 
   -1     3 const Error = error{IndexError};
   -1     4 
   -1     5 const T = u64;
   -1     6 const Box = struct {
   -1     7     x1: T,
   -1     8     x2: T,
   -1     9     y1: T,
   -1    10     y2: T,
   -1    11 };
   -1    12 
   -1    13 fn splitOnce(s: []u8, sep: u8) ![2][]u8 {
   -1    14     const i = std.mem.indexOfScalar(u8, s, sep) orelse return Error.IndexError;
   -1    15     return .{ s[0..i], s[i + 1 ..] };
   -1    16 }
   -1    17 
   -1    18 fn getBox(p1: [2]T, p2: [2]T) Box {
   -1    19     return .{
   -1    20         .x1 = @min(p1[0], p2[0]),
   -1    21         .x2 = @max(p1[0], p2[0]),
   -1    22         .y1 = @min(p1[1], p2[1]),
   -1    23         .y2 = @max(p1[1], p2[1]),
   -1    24     };
   -1    25 }
   -1    26 
   -1    27 pub fn main() !void {
   -1    28     const allocator = std.heap.smp_allocator;
   -1    29 
   -1    30     const path: [:0]const u8 = std.mem.span(std.os.argv[1]);
   -1    31     var file = try std.fs.cwd().openFile(path, .{});
   -1    32     defer file.close();
   -1    33 
   -1    34     var buffer: [1024]u8 = undefined;
   -1    35     var reader = file.reader(&buffer);
   -1    36 
   -1    37     var points = std.ArrayList([2]T).empty;
   -1    38     defer points.deinit(allocator);
   -1    39 
   -1    40     while (reader.interface.peekDelimiterExclusive('\n')) |line| {
   -1    41         reader.interface.toss(line.len + 1);
   -1    42 
   -1    43         const s1, const s2 = try splitOnce(line, ',');
   -1    44         const x = try std.fmt.parseInt(T, s1, 10);
   -1    45         const y = try std.fmt.parseInt(T, s2, 10);
   -1    46 
   -1    47         try points.append(allocator, .{ x, y });
   -1    48     } else |err| switch (err) {
   -1    49         error.EndOfStream => {},
   -1    50         else => |e| return e,
   -1    51     }
   -1    52 
   -1    53     // this task is significantly easier if the line never does weird
   -1    54     // things like walking back on itself.
   -1    55     for (0..points.items.len) |i| {
   -1    56         const p1 = points.items[i];
   -1    57         const p3 = points.items[(i + 2) % points.items.len];
   -1    58         std.debug.assert(p1[0] != p3[0]);
   -1    59         std.debug.assert(p1[1] != p3[1]);
   -1    60     }
   -1    61 
   -1    62     var part1: T = 0;
   -1    63     var part2: T = 0;
   -1    64     for (points.items, 0..) |p1, i| {
   -1    65         boxes: for (0..i) |j| {
   -1    66             const box = getBox(p1, points.items[j]);
   -1    67             const a = (box.x2 - box.x1 + 1) * (box.y2 - box.y1 + 1);
   -1    68             part1 = @max(part1, a);
   -1    69 
   -1    70             if (a > part2) {
   -1    71                 for (0..points.items.len) |k| {
   -1    72                     const edge = getBox(
   -1    73                         points.items[k],
   -1    74                         points.items[(k + 1) % points.items.len],
   -1    75                     );
   -1    76                     if (edge.x1 < box.x2 and edge.x2 > box.x1 and edge.y1 < box.y2 and edge.y2 > box.y1) {
   -1    77                         continue :boxes;
   -1    78                     }
   -1    79                 }
   -1    80                 part2 = a;
   -1    81             }
   -1    82         }
   -1    83     }
   -1    84     std.debug.print("part1: {}\n", .{part1});
   -1    85     std.debug.print("part2: {}\n", .{part2});
   -1    86 }

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

@@ -0,0 +1,8 @@
   -1     1 7,1
   -1     2 11,1
   -1     3 11,7
   -1     4 9,7
   -1     5 9,5
   -1     6 2,5
   -1     7 2,3
   -1     8 7,3