commit 08a13d416c92edb0248c3dc2789a38c9032a6fe9 Author: Nick Thomas Date: Sun Dec 1 17:40:23 2019 +0000 Day 01 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2040c29 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +zig-cache diff --git a/01/build.zig b/01/build.zig new file mode 100644 index 0000000..2ab06e7 --- /dev/null +++ b/01/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("01", "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/01/input b/01/input new file mode 100644 index 0000000..dde704b --- /dev/null +++ b/01/input @@ -0,0 +1,100 @@ +109067 +75007 +66030 +93682 +83818 +108891 +139958 +129246 +80272 +119897 +112804 +69495 +95884 +85402 +148361 +75986 +120063 +127683 +146962 +76907 +61414 +98452 +134330 +53858 +82662 +143258 +82801 +60279 +131782 +105989 +102464 +96563 +71172 +113731 +90645 +94830 +133247 +110149 +54792 +134863 +125919 +145490 +69836 +108808 +87954 +148957 +110182 +126668 +148024 +96915 +117727 +147378 +75967 +91915 +60130 +85331 +66800 +103419 +72627 +72687 +61606 +113160 +107082 +110793 +61589 +105005 +73952 +65705 +117243 +140944 +117091 +113482 +91379 +148185 +113853 +119822 +78179 +85407 +119886 +109230 +68783 +63914 +51101 +93549 +53361 +127984 +106315 +54997 +138941 +81075 +120272 +120307 +98414 +115245 +105649 +89793 +88421 +121104 +97084 +56928 diff --git a/01/src/main.zig b/01/src/main.zig new file mode 100644 index 0000000..b357db7 --- /dev/null +++ b/01/src/main.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +fn rocket(in: i64) i64 { + var x: i64 = @divFloor(in, 3) - 2; + if (x < 0) { + x = 0; + } + + return x; +} + +pub fn main() anyerror!void { + const file = std.io.getStdIn(); + const stream = &file.inStream().stream; + + var buf: [20]u8 = undefined; + var sum1: i64 = 0; // part 1 + var sum2: i64 = 0; // part 2 + + while (try stream.readUntilDelimiterOrEof(&buf, '\n')) |line| { + const mass = try std.fmt.parseInt(i64, line, 10); + var rock = rocket(mass); + sum1 += rock; + while (rock > 0) { + sum2 += rock; + rock = rocket(rock); + } + } + + std.debug.warn("Part 1: {}\n", sum1); + std.debug.warn("Part 2: {}\n", sum2); +} +