diff --git a/src/days/day02.zig b/src/days/day02.zig index 12f66ea..849737f 100644 --- a/src/days/day02.zig +++ b/src/days/day02.zig @@ -3,58 +3,25 @@ const std = @import("std"); pub const title = "Day 02"; pub fn run(_: std.mem.Allocator) !void { - const input = @embedFile("./input/day02.txt"); + const input = std.mem.trim(u8, @embedFile("./input/day02.txt"), &std.ascii.whitespace); + //const input = + // \\11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 + // ; var ranges = std.mem.tokenizeScalar(u8, input, ','); var accumulator: u64 = 0; while (ranges.next()) |range| { var components = std.mem.tokenizeScalar(u8, range, '-'); - const start_s = std.mem.trim(u8, components.next() orelse continue, &std.ascii.whitespace); - const end_s = std.mem.trim(u8, components.next() orelse continue, &std.ascii.whitespace); + + const start_s = components.next() orelse continue; + const end_s = components.next() orelse continue; + const start = try std.fmt.parseUnsigned(u64, start_s, 10); const end = try std.fmt.parseUnsigned(u64, end_s, 10); std.debug.print("{d} to {d}\n", .{start, end}); - - for (start..end+1) |id| { - std.debug.print("-- Checking {d} --\n", .{id}); - - // IDs smaller than 10 cannot possibly have repeated digits - if (id / 10 == 0) { - std.debug.print("Skipping {d}\n", .{id}); - continue; - } - - const len = getLen(id); - inner: for (2..len + 1) |divisor| { - if (len % divisor != 0) continue; - - std.debug.print("divisor = {d}\n", .{divisor}); - - const window_len = len / divisor; - - std.debug.print("window_len = {d}\n", .{window_len}); - - for (0..window_len) |i| { - const a = getDigit(id, i); - std.debug.print("a = {d}\n", .{a}); - for (1..divisor) |n| { - const b = getDigit(id, i + window_len * n); - std.debug.print("b = {d}\n", .{b}); - if (a != b) { - continue :inner; - } - } - } - // If we get here, it means id is a valid cadidate - std.debug.print("result = {d}\n", .{id}); - break; - } - accumulator += id; - } - - break; + try processRange(&accumulator, start, end); } var buffer: [64]u8 = undefined; @@ -66,6 +33,46 @@ pub fn run(_: std.mem.Allocator) !void { try stdout.flush(); } +fn processRange(accumulator: *u64, start: u64, end: u64) !void { + for (start..end+1) |id| { + std.debug.print("-- Checking {d} --\n", .{id}); + + // IDs smaller than 10 cannot possibly have repeated digits + if (id / 10 == 0) { + std.debug.print("Skipping {d}\n", .{id}); + continue; + } + + const len = getLen(id); + inner: for (2..len + 1) |divisor| { + if (len % divisor != 0) continue; + + std.debug.print("divisor = {d}\n", .{divisor}); + + const offset = len / divisor; + + std.debug.print("offset = {d}\n", .{offset}); + + for (0..offset) |i| { + const a = getDigit(id, i); + std.debug.print("a = {d}\n", .{a}); + for (1..divisor) |n| { + const b = getDigit(id, i + offset * n); + std.debug.print("b = {d}\n", .{b}); + if (a != b) { + continue :inner; + } + } + } + + // If we get here, it means id is a valid cadidate + std.debug.print("result = {d}\n", .{id}); + accumulator.* += id; + break; + } + } +} + fn getDigit(x: u64, n: usize) u8 { var new_x = x; for (0..n) |_| {