diff --git a/src/days/day02.zig b/src/days/day02.zig index 849737f..4de3c68 100644 --- a/src/days/day02.zig +++ b/src/days/day02.zig @@ -3,92 +3,5 @@ const std = @import("std"); pub const title = "Day 02"; pub fn run(_: std.mem.Allocator) !void { - 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 = 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}); - try processRange(&accumulator, start, end); - } - - var buffer: [64]u8 = undefined; - var stdout_writer = std.fs.File.stdout().writer(&buffer); - const stdout = &stdout_writer.interface; - - try stdout.print("{d}\n", .{accumulator}); - - 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) |_| { - new_x /= 10; - } - return @intCast(new_x % 10); -} - -fn getLen(x: u64) usize { - var new_x = x; - var n: usize = 0; - while (true) { - n += 1; - new_x /= 10; - if (new_x == 0) break; - } - return n; } diff --git a/src/days/input/day02.txt b/src/days/input/day02.txt deleted file mode 100755 index 69086d8..0000000 --- a/src/days/input/day02.txt +++ /dev/null @@ -1 +0,0 @@ -52500467-52574194,655624494-655688785,551225-576932,8418349387-8418411293,678-1464,33-79,74691-118637,8787869169-8787890635,9898977468-9899009083,548472423-548598890,337245835-337375280,482823-543075,926266-991539,1642682920-1642753675,3834997-3940764,1519-2653,39697698-39890329,3-21,3251796-3429874,3467-9298,26220798-26290827,80-124,200638-280634,666386-710754,21329-64315,250-528,9202893-9264498,819775-903385,292490-356024,22-32,2663033-2791382,133-239,56514707-56704320,432810-458773,4949427889-4949576808