diff --git a/day2/build.zig b/day2/build.zig new file mode 100644 index 0000000..2a0da41 --- /dev/null +++ b/day2/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("day2", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/day2/input b/day2/input new file mode 100644 index 0000000..62cb6df --- /dev/null +++ b/day2/input @@ -0,0 +1,1000 @@ +forward 4 +down 8 +down 8 +up 2 +up 7 +forward 5 +forward 5 +up 7 +down 6 +down 3 +down 1 +forward 5 +forward 9 +up 2 +down 9 +forward 4 +up 5 +forward 7 +down 2 +forward 7 +down 2 +forward 4 +up 3 +down 9 +up 8 +down 2 +down 6 +up 1 +forward 3 +down 6 +down 2 +forward 9 +up 1 +forward 5 +down 1 +forward 2 +up 2 +forward 4 +down 3 +down 8 +up 2 +down 3 +up 4 +down 8 +forward 7 +forward 9 +down 7 +down 1 +forward 5 +up 3 +down 6 +down 6 +forward 1 +down 9 +forward 6 +forward 9 +forward 2 +forward 5 +forward 7 +down 1 +up 6 +up 7 +forward 8 +forward 6 +forward 2 +down 5 +up 3 +up 4 +down 9 +up 4 +down 9 +up 4 +down 5 +forward 3 +down 8 +up 2 +down 2 +forward 7 +down 7 +forward 6 +down 2 +forward 5 +down 1 +forward 9 +down 9 +down 5 +forward 2 +forward 3 +forward 6 +forward 1 +down 8 +forward 2 +forward 1 +forward 9 +down 8 +forward 8 +up 1 +up 2 +forward 2 +forward 7 +down 2 +up 9 +forward 5 +forward 5 +up 5 +down 1 +up 8 +forward 3 +up 5 +forward 2 +up 8 +up 7 +forward 4 +down 6 +up 1 +up 6 +forward 5 +down 8 +forward 4 +down 7 +forward 5 +down 4 +down 9 +forward 2 +down 5 +down 2 +down 3 +forward 8 +down 8 +down 2 +down 5 +down 6 +up 8 +down 1 +up 7 +up 4 +up 1 +up 6 +forward 6 +forward 6 +forward 8 +up 5 +forward 4 +forward 5 +forward 3 +down 8 +forward 9 +forward 6 +forward 6 +up 1 +up 8 +forward 2 +up 9 +down 1 +up 7 +up 3 +down 3 +forward 2 +down 5 +up 8 +forward 3 +up 5 +down 3 +down 3 +up 7 +forward 2 +forward 3 +forward 6 +forward 9 +up 3 +forward 1 +up 9 +down 8 +forward 5 +down 8 +forward 9 +down 1 +forward 7 +forward 9 +forward 2 +down 6 +up 6 +down 2 +down 1 +forward 7 +down 3 +forward 3 +down 3 +forward 1 +forward 6 +forward 1 +down 4 +down 4 +down 5 +forward 3 +forward 1 +up 8 +forward 7 +down 6 +up 6 +down 5 +up 6 +down 3 +down 8 +down 9 +forward 2 +up 8 +forward 1 +forward 2 +forward 7 +forward 5 +up 6 +down 9 +up 9 +forward 7 +forward 6 +forward 7 +down 8 +down 6 +forward 5 +down 2 +down 5 +down 3 +down 4 +up 5 +down 5 +forward 7 +forward 2 +down 1 +forward 6 +up 8 +down 3 +down 5 +down 3 +forward 3 +up 2 +forward 9 +forward 2 +up 4 +down 3 +down 7 +forward 9 +forward 6 +up 1 +up 2 +down 5 +up 8 +forward 9 +forward 2 +down 3 +down 6 +up 3 +down 9 +down 2 +up 4 +down 3 +up 7 +forward 3 +up 9 +down 3 +down 9 +down 1 +down 1 +forward 7 +down 9 +forward 3 +up 6 +down 8 +down 3 +forward 7 +forward 1 +up 4 +forward 8 +forward 1 +forward 9 +up 9 +forward 4 +up 2 +down 6 +down 5 +down 8 +down 2 +down 4 +forward 5 +down 8 +down 1 +forward 5 +forward 9 +down 4 +forward 5 +forward 4 +forward 4 +up 6 +down 7 +down 2 +forward 8 +down 7 +forward 7 +forward 7 +forward 3 +down 3 +forward 6 +down 5 +down 5 +forward 3 +down 7 +up 3 +up 6 +forward 8 +down 3 +down 6 +forward 5 +forward 4 +down 4 +down 3 +down 1 +down 4 +down 2 +forward 1 +forward 5 +down 9 +forward 8 +down 7 +forward 4 +down 5 +down 5 +forward 7 +forward 9 +down 5 +down 8 +up 9 +forward 1 +down 9 +up 1 +down 8 +forward 4 +up 8 +up 7 +down 4 +forward 2 +forward 9 +up 9 +forward 4 +forward 5 +forward 5 +forward 4 +forward 4 +down 8 +forward 3 +forward 3 +forward 1 +forward 7 +forward 7 +up 2 +forward 9 +down 8 +forward 3 +down 3 +down 3 +down 4 +forward 9 +forward 9 +forward 7 +forward 9 +down 6 +forward 6 +down 4 +forward 7 +down 3 +forward 2 +down 9 +down 9 +up 2 +down 7 +down 6 +up 5 +forward 6 +forward 5 +down 9 +forward 8 +down 9 +forward 9 +down 7 +up 8 +forward 5 +forward 1 +down 5 +forward 1 +down 4 +up 6 +up 1 +down 5 +forward 3 +down 1 +up 7 +down 8 +up 5 +down 8 +up 6 +forward 6 +down 8 +up 2 +forward 5 +down 5 +down 7 +down 7 +forward 8 +forward 6 +forward 2 +forward 3 +forward 3 +forward 9 +down 7 +up 8 +up 1 +forward 8 +down 5 +down 7 +forward 2 +down 9 +down 5 +down 5 +forward 6 +forward 1 +forward 8 +down 3 +down 3 +down 7 +up 3 +down 3 +down 5 +down 1 +forward 3 +forward 2 +forward 4 +forward 1 +forward 3 +forward 6 +down 6 +down 4 +forward 2 +down 8 +up 1 +down 7 +down 6 +down 3 +down 6 +forward 8 +up 7 +down 7 +up 7 +down 1 +forward 2 +forward 9 +up 8 +down 2 +down 3 +down 7 +down 2 +up 2 +down 1 +down 7 +up 6 +down 4 +forward 9 +down 8 +down 1 +forward 5 +forward 1 +up 7 +up 9 +up 9 +down 5 +down 7 +down 2 +down 6 +down 3 +forward 8 +forward 4 +up 3 +down 9 +up 3 +down 6 +up 8 +forward 7 +down 7 +up 5 +down 1 +down 3 +up 4 +forward 2 +down 7 +down 3 +down 7 +up 1 +forward 8 +down 3 +forward 7 +down 8 +forward 5 +forward 8 +down 8 +up 4 +up 8 +forward 3 +down 7 +up 6 +down 9 +forward 4 +forward 4 +forward 3 +up 4 +down 4 +down 7 +forward 6 +down 7 +down 8 +up 5 +down 4 +up 6 +up 6 +up 4 +down 7 +forward 7 +up 4 +down 2 +up 2 +forward 6 +down 5 +down 1 +forward 2 +up 1 +down 4 +up 2 +down 7 +down 5 +up 5 +forward 6 +up 2 +forward 2 +up 9 +up 4 +down 1 +down 3 +up 7 +up 5 +down 9 +down 2 +forward 9 +down 1 +up 9 +down 4 +down 8 +forward 3 +forward 1 +forward 4 +forward 9 +down 5 +down 5 +down 8 +up 4 +up 1 +down 9 +up 4 +forward 9 +up 1 +forward 7 +down 4 +up 2 +down 1 +forward 9 +down 9 +down 2 +forward 8 +up 2 +forward 6 +down 1 +up 9 +down 3 +down 2 +down 8 +down 2 +forward 8 +forward 2 +forward 8 +down 3 +up 6 +forward 5 +forward 4 +forward 7 +forward 1 +down 8 +forward 7 +down 9 +up 7 +up 5 +forward 1 +down 6 +down 6 +up 9 +up 9 +up 1 +forward 1 +forward 5 +up 1 +forward 2 +down 8 +up 9 +forward 2 +forward 8 +down 2 +up 5 +up 9 +down 5 +forward 2 +forward 4 +forward 2 +up 7 +down 9 +forward 5 +down 1 +down 6 +up 1 +forward 8 +down 1 +down 7 +up 2 +forward 4 +down 2 +up 6 +forward 6 +forward 3 +down 3 +forward 2 +down 2 +up 9 +forward 2 +up 1 +down 9 +down 4 +up 8 +forward 3 +down 9 +down 9 +forward 9 +forward 8 +up 8 +down 8 +up 8 +forward 4 +down 9 +up 5 +forward 8 +up 6 +forward 7 +up 6 +down 2 +down 3 +forward 9 +forward 5 +down 6 +forward 9 +down 5 +down 9 +down 7 +down 9 +down 3 +forward 4 +forward 2 +down 2 +down 7 +down 7 +up 2 +up 3 +forward 6 +up 7 +forward 4 +down 3 +forward 2 +down 1 +down 8 +forward 5 +down 3 +up 9 +forward 2 +forward 7 +down 4 +forward 1 +forward 8 +forward 9 +forward 5 +down 4 +up 3 +up 9 +forward 6 +forward 4 +forward 9 +down 3 +forward 1 +forward 9 +down 9 +down 5 +forward 9 +forward 4 +down 3 +down 9 +down 5 +up 6 +up 5 +forward 5 +up 8 +down 3 +forward 7 +up 3 +forward 9 +down 8 +forward 2 +forward 1 +forward 9 +down 9 +forward 1 +down 6 +forward 7 +up 3 +forward 7 +up 3 +down 1 +forward 5 +forward 5 +up 3 +forward 2 +down 3 +forward 8 +up 9 +forward 7 +down 7 +forward 5 +up 4 +forward 8 +down 1 +up 4 +down 2 +forward 2 +down 5 +down 5 +up 2 +forward 1 +down 3 +down 8 +forward 6 +forward 6 +down 5 +up 4 +down 7 +down 9 +up 9 +forward 7 +forward 4 +down 7 +down 5 +down 2 +down 9 +down 6 +down 7 +up 6 +up 7 +up 6 +down 4 +forward 9 +down 8 +down 7 +down 8 +down 4 +forward 5 +forward 1 +up 5 +forward 5 +forward 4 +down 3 +forward 8 +down 7 +down 9 +up 1 +down 1 +up 8 +up 6 +down 9 +up 9 +down 9 +forward 7 +down 3 +forward 6 +down 6 +forward 6 +down 9 +down 7 +up 1 +down 2 +up 2 +down 3 +down 1 +up 4 +forward 3 +down 3 +up 8 +down 3 +forward 3 +forward 6 +forward 6 +forward 6 +forward 7 +up 2 +forward 6 +forward 1 +up 4 +up 7 +down 5 +down 9 +forward 6 +down 4 +forward 6 +down 7 +down 2 +up 9 +up 3 +forward 8 +forward 5 +down 1 +down 6 +down 7 +down 5 +up 3 +up 9 +forward 2 +forward 5 +down 3 +down 2 +up 2 +forward 6 +forward 3 +down 8 +forward 7 +up 6 +forward 4 +down 8 +forward 6 +down 7 +forward 9 +forward 6 +forward 2 +forward 4 +up 5 +up 1 +forward 3 +forward 2 +up 3 +down 4 +down 3 +down 1 +up 8 +forward 6 +down 4 +down 9 +down 3 +up 8 +down 5 +forward 2 +down 3 +up 7 +down 3 +up 1 +up 1 +up 2 +up 1 +forward 4 +forward 1 +forward 4 +forward 3 +forward 8 +down 8 +up 5 +down 4 +down 4 +down 6 +down 9 +down 7 +forward 5 +forward 3 +up 3 +forward 6 +forward 5 +forward 2 +forward 6 +up 4 +forward 2 +up 3 +down 2 +forward 3 +down 8 +forward 1 +forward 2 +down 3 +down 5 +forward 6 +forward 3 +forward 6 +up 3 +forward 5 +forward 3 +forward 5 +down 6 +down 4 +down 4 +forward 3 +forward 3 +up 6 +up 8 +forward 5 +forward 1 +down 3 +down 8 +down 9 +up 3 +down 7 +forward 4 +forward 2 +down 2 +up 6 +down 1 +down 8 +forward 3 +up 1 +down 7 +down 7 +down 5 +forward 3 +down 8 +forward 3 +down 7 +down 5 +up 2 +forward 9 +down 8 +down 5 +forward 3 +forward 2 +forward 7 +up 8 +down 2 +down 5 +down 8 +down 9 +down 9 +down 1 +up 4 +forward 5 +up 1 +up 4 +forward 1 +down 1 +down 7 +up 9 +up 7 +down 5 +down 9 +down 9 +down 8 +forward 7 +down 3 +up 4 +down 7 +down 8 +forward 7 +forward 4 +up 9 +down 2 +up 7 +forward 5 +down 3 +forward 3 +forward 5 +forward 5 +down 2 +down 2 +down 7 +up 8 +up 9 +down 1 +forward 9 +forward 3 +up 3 +forward 9 +up 2 +down 7 +down 3 +forward 4 +down 5 +down 3 +up 5 +forward 4 diff --git a/day2/result b/day2/result new file mode 100644 index 0000000..bebffcf --- /dev/null +++ b/day2/result @@ -0,0 +1,2 @@ +info: [Part 1] Depth 916, Horizontal Position 1845, Sum 1690020 +info: [Part 2] Depth 763408, Horizontal Position 1845, Sum 1408487760 diff --git a/day2/src/main.zig b/day2/src/main.zig new file mode 100644 index 0000000..943d118 --- /dev/null +++ b/day2/src/main.zig @@ -0,0 +1,58 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const alloc = &arena.allocator; + + // Read our input + var f = try std.fs.cwd().openFile("input", .{}); + defer f.close(); + var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); + defer alloc.free(contents); + + // Part 1 + var pos_x: i32 = 0; + var depth: i32 = 0; + + var it = std.mem.tokenize(contents, "\n"); + while (it.next()) |line| { + var lit = std.mem.tokenize(line, " "); + var dir = lit.next().?; + var amount = try std.fmt.parseInt(i32, lit.next().?, 10); + if (std.mem.eql(u8, dir, "up")) { + depth -= amount; + } + else if (std.mem.eql(u8, dir, "forward")) { + pos_x += amount; + } + else if (std.mem.eql(u8, dir, "down")) { + depth += amount; + } + } + std.log.info("[Part 1] Depth {}, Horizontal Position {}, Sum {}", + .{depth, pos_x, depth * pos_x}); + + // Part 2 + pos_x = 0; + var aim: i32 = 0; + depth = 0; + it = std.mem.tokenize(contents, "\n"); + while (it.next()) |line| { + var lit = std.mem.tokenize(line, " "); + var dir = lit.next().?; + var amount = try std.fmt.parseInt(i32, lit.next().?, 10); + if (std.mem.eql(u8, dir, "up")) { + aim -= amount; + } + else if (std.mem.eql(u8, dir, "forward")) { + depth += amount * aim; + pos_x += amount; + } + else if (std.mem.eql(u8, dir, "down")) { + aim += amount; + } + } + std.log.info("[Part 2] Depth {}, Horizontal Position {}, Sum {}", + .{depth, pos_x, depth * pos_x}); +}