Advent of Code 2025 - Zig
at main 916 B view raw
1const std = @import("std"); 2const Allocator = std.mem.Allocator; 3 4pub fn run(_: Allocator, input: []u8) !struct { u64, u64 } { 5 var lines = std.mem.tokenizeScalar(u8, input, '\n'); 6 7 var answer1: u64 = 0; 8 var answer2: u64 = 0; 9 while (lines.next()) |line| { 10 answer1 += try calculateJoltage(line, 2); 11 answer2 += try calculateJoltage(line, 12); 12 } 13 14 return .{ answer1, answer2 }; 15} 16 17fn calculateJoltage(line: []const u8, num_digits: usize) !u64 { 18 var index: usize = 0; 19 var value: usize = 0; 20 var total: usize = 0; 21 for (0..num_digits) |i| { 22 const start = if (i == 0) 0 else index + 1; 23 const end = line.len - (num_digits - i - 1); 24 25 index = std.mem.indexOfMax(u8, line[start..end]) + start; 26 value = try std.fmt.charToDigit(line[index], 10); 27 28 total += value * std.math.pow(usize, 10, num_digits - i - 1); 29 } 30 31 return total; 32}