tangled
alpha
login
or
join now
nesv.ca
/
advent-of-code-2025
1
fork
atom
My Advent of Code 2025 solutions
1
fork
atom
overview
issues
pulls
pipelines
Day 3: Lobby
nesv.ca
3 months ago
e6a95d53
bfa53d8d
+67
1 changed file
expand all
collapse all
unified
split
src
day03.zig
+67
src/day03.zig
···
1
1
+
const std = @import("std");
2
2
+
const mem = std.mem;
3
3
+
const testing = std.testing;
4
4
+
const aoc = @import("advent_of_code_2025");
5
5
+
6
6
+
const input = @embedFile("input03.txt");
7
7
+
8
8
+
pub fn main() !void {
9
9
+
var max_joltage_2: u64 = 0;
10
10
+
var max_joltage_12: u64 = 0;
11
11
+
var banks = mem.tokenizeScalar(u8, input, '\n');
12
12
+
while (banks.next()) |bank| {
13
13
+
max_joltage_2 += maxJoltage(bank, 2);
14
14
+
max_joltage_12 += maxJoltage(bank, 12);
15
15
+
}
16
16
+
17
17
+
std.debug.print("{d}\n{d}\n", .{
18
18
+
max_joltage_2,
19
19
+
max_joltage_12,
20
20
+
});
21
21
+
}
22
22
+
23
23
+
// Find the maximum joltage rating for a battery bank by enabling the specified
24
24
+
// number of batteries.
25
25
+
fn maxJoltage(bank: []const u8, num_batteries: usize) u64 {
26
26
+
var max: u64 = 0;
27
27
+
var start: usize = 0;
28
28
+
29
29
+
var remaining = num_batteries;
30
30
+
while (remaining > 0) : (remaining -= 1) {
31
31
+
// Take a slice of the battery bank, so that we are only looking at
32
32
+
// `bank.len - remaining` values. In the best-case scenario, the
33
33
+
// largest value we find is at the end of the slice.
34
34
+
const end = bank.len - remaining + 1;
35
35
+
const batteries = bank[start..end];
36
36
+
37
37
+
const index = mem.indexOfMax(u8, batteries);
38
38
+
39
39
+
const joltage: u64 = @intCast(batteries[index] - '0');
40
40
+
max = (max * 10) + joltage;
41
41
+
42
42
+
// On the next iteration, start at the index after the current max
43
43
+
// value.
44
44
+
start += index + 1;
45
45
+
}
46
46
+
47
47
+
return max;
48
48
+
}
49
49
+
50
50
+
const test_input = [_][]const u8{
51
51
+
"987654321111111",
52
52
+
"811111111111119",
53
53
+
"234234234234278",
54
54
+
"818181911112111",
55
55
+
};
56
56
+
57
57
+
test maxJoltage {
58
58
+
try testing.expectEqual(98, maxJoltage(test_input[0], 2));
59
59
+
try testing.expectEqual(89, maxJoltage(test_input[1], 2));
60
60
+
try testing.expectEqual(78, maxJoltage(test_input[2], 2));
61
61
+
try testing.expectEqual(92, maxJoltage(test_input[3], 2));
62
62
+
63
63
+
try testing.expectEqual(987654321111, maxJoltage(test_input[0], 12));
64
64
+
try testing.expectEqual(811111111119, maxJoltage(test_input[1], 12));
65
65
+
try testing.expectEqual(434234234278, maxJoltage(test_input[2], 12));
66
66
+
try testing.expectEqual(888911112111, maxJoltage(test_input[3], 12));
67
67
+
}