aoc2025 @ master

  1const std = @import("std");
  2
  3const board_height: u64 = 5;
  4const board_width: u64 = 3785;
  5
  6pub fn pt1(input: []const u8) u64 {
  7    var x: usize = 0;
  8    var counter: u64 = 0;
  9    while (x < board_width) {
 10        const width = getChunkSize(input, x);
 11        counter += calulateChunckPt1(input, x, width);
 12        x += width + 1;
 13    }
 14    return counter;
 15}
 16
 17pub fn pt2(input: []const u8) u64 {
 18    var x: usize = 0;
 19    var counter: u64 = 0;
 20    while (x < board_width) {
 21        const width = getChunkSize(input, x);
 22        counter += calulateChunckPt2(input, x, width);
 23        x += width + 1;
 24    }
 25    return counter;
 26}
 27
 28fn get(input: []const u8, x: usize, y: usize) u8 {
 29    return input[x + (y * board_width)];
 30}
 31
 32fn getChunkSize(input: []const u8, x: usize) usize {
 33    for (x..board_width) |w| {
 34        var empty_count: usize = 0;
 35        for (0..board_height) |h| {
 36            const c = get(input, w, h);
 37            if (c == ' ' or c == '\n') empty_count += 1;
 38        }
 39
 40        if (empty_count == board_height) {
 41            return w - x;
 42        }
 43    }
 44
 45    unreachable;
 46}
 47
 48fn calulateChunckPt1(input: []const u8, x: usize, width: usize) u64 {
 49    var numbers: [board_height - 1]u64 = undefined;
 50
 51    for (0..(board_height - 1)) |h| {
 52        const from = x + (h * board_width);
 53        const to = x + (h * board_width) + width;
 54        const number_str = input[from..to];
 55        const trimed_number_str = std.mem.trim(u8, number_str, " ");
 56        const number = std.fmt.parseInt(u64, trimed_number_str, 10) catch unreachable;
 57        numbers[h] = number;
 58    }
 59
 60    var sum = true;
 61
 62    for (0..width) |w| {
 63        const c = get(input, w + x, board_height - 1);
 64        if (c == '*') {
 65            sum = !sum;
 66            break;
 67        }
 68    }
 69
 70    if (sum) {
 71        var counter: u64 = 0;
 72        for (numbers) |n| {
 73            counter += n;
 74        }
 75        return counter;
 76    } else {
 77        var counter: u64 = 1;
 78        for (numbers) |n| {
 79            counter *= n;
 80        }
 81        return counter;
 82    }
 83}
 84
 85fn calulateChunckPt2(input: []const u8, x: usize, width: usize) u64 {
 86    // guessing a higher number of column so I don't have to alloc
 87    var numbers: [10]u64 = undefined;
 88
 89    var number_str: [board_height - 1]u8 = undefined;
 90
 91    for (x..(x + width), 0..) |w, i| {
 92        for (0..board_height - 1) |h| {
 93            const c = get(input, w, h);
 94            number_str[h] = c;
 95        }
 96
 97        const trimed_number_str = std.mem.trim(u8, &number_str, " ");
 98        const number = std.fmt.parseInt(u64, trimed_number_str, 10) catch unreachable;
 99        numbers[i] = number;
100    }
101
102    var sum = true;
103
104    for (0..width) |w| {
105        const c = get(input, w + x, board_height - 1);
106        if (c == '*') {
107            sum = !sum;
108            break;
109        }
110    }
111
112    if (sum) {
113        var counter: u64 = 0;
114        for (0..width) |n| {
115            counter += numbers[n];
116        }
117        return counter;
118    } else {
119        var counter: u64 = 1;
120        for (0..width) |n| {
121            counter *= numbers[n];
122        }
123        return counter;
124    }
125}
126
127test "Day 6 part 1" {
128    const res = pt1(@embedFile("./input/day6"));
129    try std.testing.expect(res == 6757749566978);
130}
131
132test "Day 6 part 2" {
133    const res = pt2(@embedFile("./input/day6"));
134    try std.testing.expect(res == 10603075273949);
135}