My Advent of Code 2025 solutions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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