diff --git a/build.zig b/build.zig index 802c926..9a9dab4 100644 --- a/build.zig +++ b/build.zig @@ -4,18 +4,23 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + addDay("day1", b, target, optimize); + addDay("day2", b, target, optimize); +} + +fn addDay(comptime name: []const u8, b: *std.Build, target: anytype, optimize: anytype) void { const exe = b.addExecutable(.{ - .name = "day1", + .name = name, .root_module = b.createModule(.{ .optimize = optimize, .target = target, - .root_source_file = b.path("day1.zig"), + .root_source_file = b.path(name ++ ".zig"), }), }); b.installArtifact(exe); const run = b.addRunArtifact(exe); - const step = b.step("day1", "run day"); + const step = b.step(name, "run " ++ name); step.dependOn(&run.step); if (b.args) |args| { diff --git a/day2.zig b/day2.zig new file mode 100644 index 0000000..9d58cd1 --- /dev/null +++ b/day2.zig @@ -0,0 +1,65 @@ +const std = @import("std"); +const Io = std.Io; + +const shared = @import("shared.zig"); +const print = shared.print; + +var buf: [4096]u8 = undefined; + +fn repeatCountFromStart(needle: []const u8, haystack: []const u8) usize { + var off: usize = 0; + var count: usize = 0; + while (off + needle.len <= haystack.len and std.mem.eql(u8, needle, haystack[off .. off + needle.len])) { + count += 1; + off += needle.len; + } + return count; +} + +pub fn main(init: std.process.Init) !void { + try shared.sharedInit(init); + const allocator: std.mem.Allocator = init.arena.allocator(); + const bytes = shared.loadBytesArg(allocator); + + var tok = std.mem.tokenizeScalar(u8, bytes, ','); + var part1: i64 = 0; + var part2: i64 = 0; + + while (tok.next()) |next| { + var splitIter = std.mem.splitAny(u8, next, "-"); + var v: [2]i64 = .{ 0, 0 }; + var i: usize = 0; + + while (splitIter.next()) |s| { + v[i] = try std.fmt.parseInt(i64, s, 10); + i += 1; + if (i > 2) unreachable; + } + + for (@intCast(v[0])..@intCast(v[1] + 1)) |j| { + const s = try std.fmt.bufPrint(&buf, "{d}", .{j}); + const left = s[0..(s.len >> 1)]; + if (s.len % 2 == 0 and shared.countInstancesOf(s, left) == 2) { + part1 += @intCast(j); + } + + { + var h: usize = s.len + 1; + + while (h - 1 > 0) : (h -= 1) { + const count = repeatCountFromStart(s[0 .. h - 1], s); + // print("{s} {d} count{d} s.len{d}", .{ s[0 .. h - 1], j, count, s.len }); + if (count * (h - 1) == s.len and count != 1) { + // print("{s} yay! {d} count{d}", .{ s[0 .. h - 1], j, count }); + part2 += @intCast(j); + break; + } else { + //print("{s}", .{s[0..h]}); + } + } + } + } + } + + print("part1 {d} part2 {d}", .{ part1, part2 }); +} diff --git a/day2/input.txt b/day2/input.txt new file mode 100644 index 0000000..13a9ed2 --- /dev/null +++ b/day2/input.txt @@ -0,0 +1 @@ +119-210,907313-1048019,7272640820-7272795557,6315717352-6315818282,42-65,2234869-2439411,1474-2883,33023-53147,1-15,6151-14081,3068-5955,65808-128089,518490556-518593948,3535333552-3535383752,7340190-7548414,547-889,34283147-34389716,44361695-44519217,607492-669180,7071078-7183353,67-115,969-1469,3636264326-3636424525,762682710-762831570,827113-906870,205757-331544,290-523,86343460-86510016,5536957-5589517,132876-197570,676083-793651,23-41,17920-31734,440069-593347 diff --git a/day2/test.txt b/day2/test.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/day2/test.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/shared.zig b/shared.zig index 497b581..8a9c4a3 100644 --- a/shared.zig +++ b/shared.zig @@ -98,3 +98,25 @@ pub const LineIter = struct { return self.toSlice(bytes); } }; + +pub fn countInstancesOf(haystack: []const u8, needle: []const u8) usize { + var count: usize = 0; + var i: usize = 0; + while (i < haystack.len) : (i += 1) { + if (std.mem.eql(u8, needle, haystack[i..@min(haystack.len, i + needle.len)])) { + count += 1; + i += needle.len - 1; + } + } + return count; +} + +pub fn countSequencesAt(off: usize, needle: []const u8, haystack: []const u8) usize { + var count: usize = 1; + var offset = off; + while (offset + needle.len < haystack.len and std.mem.eql(u8, needle, haystack[offset .. needle.len + offset])) { + offset += needle.len; + count += 1; + } + return count; +}