adventofcode

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

commit
9945b0a73adbee949695bbbce968d3601d20e022
parent
532c0fb2665fd66f907de67e296885cdf91715b3
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-12-10 21:04
2025-12-08

Diffstat

A 2025/08/solution.zig 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2025/08/test.txt 20 ++++++++++++++++++++

2 files changed, 118 insertions, 0 deletions


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

@@ -0,0 +1,98 @@
   -1     1 const std = @import("std");
   -1     2 
   -1     3 const Error = error{IndexError};
   -1     4 
   -1     5 const nPoints = 1000;
   -1     6 const nConnections = 1000;
   -1     7 
   -1     8 const Point = [3]i64;
   -1     9 const Connection = struct {
   -1    10     d: i64,
   -1    11     i: usize,
   -1    12     j: usize,
   -1    13 };
   -1    14 
   -1    15 fn parsePoint(s: []u8) !Point {
   -1    16     const i = std.mem.indexOfScalar(u8, s, ',') orelse return Error.IndexError;
   -1    17     const tail = s[i + 1 ..];
   -1    18     const j = std.mem.indexOfScalar(u8, tail, ',') orelse return Error.IndexError;
   -1    19     const x = try std.fmt.parseInt(i64, s[0..i], 10);
   -1    20     const y = try std.fmt.parseInt(i64, tail[0..j], 10);
   -1    21     const z = try std.fmt.parseInt(i64, tail[j + 1 ..], 10);
   -1    22     return .{ x, y, z };
   -1    23 }
   -1    24 
   -1    25 fn dist(a: Point, b: Point) i64 {
   -1    26     var result: i64 = 0;
   -1    27     for (0..3) |i| {
   -1    28         result += (a[i] - b[i]) * (a[i] - b[i]);
   -1    29     }
   -1    30     return result;
   -1    31 }
   -1    32 
   -1    33 fn connectionAsc(_: void, a: Connection, b: Connection) bool {
   -1    34     return a.d < b.d;
   -1    35 }
   -1    36 
   -1    37 pub fn main() !void {
   -1    38     const path: [:0]const u8 = std.mem.span(std.os.argv[1]);
   -1    39     var file = try std.fs.cwd().openFile(path, .{});
   -1    40     defer file.close();
   -1    41 
   -1    42     var buffer: [1024]u8 = undefined;
   -1    43     var reader = file.reader(&buffer);
   -1    44 
   -1    45     var i: usize = 0;
   -1    46     var k: usize = 0;
   -1    47     var points = [_]Point{.{ 0, 0, 0 }} ** nPoints;
   -1    48     var connections = [_]Connection{undefined} ** (nPoints * (nPoints - 1) / 2);
   -1    49 
   -1    50     while (reader.interface.peekDelimiterExclusive('\n')) |line| {
   -1    51         reader.interface.toss(line.len + 1);
   -1    52         points[i] = try parsePoint(line);
   -1    53         for (0..i) |j| {
   -1    54             const d = dist(points[i], points[j]);
   -1    55             connections[k] = Connection{ .d = d, .i = i, .j = j };
   -1    56             k += 1;
   -1    57         }
   -1    58         i += 1;
   -1    59     } else |err| switch (err) {
   -1    60         error.EndOfStream => {},
   -1    61         else => |e| return e,
   -1    62     }
   -1    63 
   -1    64     std.sort.block(Connection, &connections, void{}, connectionAsc);
   -1    65 
   -1    66     // each cluster is identified by its lowest member
   -1    67     var counts = [_]usize{1} ** nPoints;
   -1    68     var clusters = [_]usize{0} ** nPoints;
   -1    69     for (0..clusters.len) |j| {
   -1    70         clusters[j] = j;
   -1    71     }
   -1    72 
   -1    73     for (&connections, 0..) |c, n| {
   -1    74         if (clusters[c.i] != clusters[c.j]) {
   -1    75             const cmin = @min(clusters[c.i], clusters[c.j]);
   -1    76             const cmax = @max(clusters[c.i], clusters[c.j]);
   -1    77 
   -1    78             for (0..clusters.len) |j| {
   -1    79                 if (clusters[j] == cmax) {
   -1    80                     clusters[j] = cmin;
   -1    81                 }
   -1    82             }
   -1    83             counts[cmin] += counts[cmax];
   -1    84             counts[cmax] = 0;
   -1    85 
   -1    86             if (counts[cmin] == nPoints) {
   -1    87                 std.debug.print("part2: {}\n", .{points[c.i][0] * points[c.j][0]});
   -1    88                 break;
   -1    89             }
   -1    90         }
   -1    91 
   -1    92         if (n + 1 == nConnections) {
   -1    93             var copy = counts;
   -1    94             std.sort.block(usize, &copy, void{}, std.sort.desc(usize));
   -1    95             std.debug.print("part1: {}\n", .{copy[0] * copy[1] * copy[2]});
   -1    96         }
   -1    97     }
   -1    98 }

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

@@ -0,0 +1,20 @@
   -1     1 162,817,812
   -1     2 57,618,57
   -1     3 906,360,560
   -1     4 592,479,940
   -1     5 352,342,300
   -1     6 466,668,158
   -1     7 542,29,236
   -1     8 431,825,988
   -1     9 739,650,466
   -1    10 52,470,668
   -1    11 216,146,977
   -1    12 819,987,18
   -1    13 117,168,530
   -1    14 805,96,715
   -1    15 346,949,466
   -1    16 970,615,88
   -1    17 941,993,340
   -1    18 862,61,35
   -1    19 984,92,344
   -1    20 425,690,689