diff --git a/04/build.zig b/04/build.zig new file mode 100644 index 0000000..843c820 --- /dev/null +++ b/04/build.zig @@ -0,0 +1,14 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + const exe = b.addExecutable("04", "src/main.zig"); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/04/src/main.zig b/04/src/main.zig new file mode 100644 index 0000000..c5310cf --- /dev/null +++ b/04/src/main.zig @@ -0,0 +1,42 @@ +const std = @import("std"); + +const first = 171309; +const last = 643603; + +// - [x] It is a six-digit number (implied) +// - [x] The value is within the range given in your puzzle input (implied) +// - [x] Two adjacent digits are the same (like 22 in 122345). +// - [x] Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679). + +fn tests1(num: i32) anyerror!bool { + var buf: [6]u8 = undefined; + var numStr = try std.fmt.bufPrint(&buf, "{}", num); + + if (numStr[0] > numStr[1] or + numStr[1] > numStr[2] or + numStr[2] > numStr[3] or + numStr[3] > numStr[4] or + numStr[4] > numStr[5] + ) return false; + + return + numStr[0] == numStr[1] or + numStr[1] == numStr[2] or + numStr[2] == numStr[3] or + numStr[3] == numStr[4] or + numStr[4] == numStr[5]; +} + +pub fn main() anyerror!void { + var count: i32 = 0; + var i: i32 = first; + + while (i <= last) : (i+=1) { + if (try tests1(i)) { + //std.debug.warn("Match: {}\n", i); + count+=1; + } + } + + std.debug.warn("Part 1: matches: {}\n", count); +}