finished day 2
This commit is contained in:
parent
da7d29c2c7
commit
c9e4d62c12
11
build.zig
11
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| {
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
22
shared.zig
22
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue