From 1db33f5a9f9fd22ebb3d947bcdc559ff0f9577d3 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Mon, 6 Dec 2021 08:58:45 -0500 Subject: [PATCH] Day 6 --- day6/build.zig | 27 +++++++++++++ day6/input | 1 + day6/src/main.zig | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 day6/build.zig create mode 100644 day6/input create mode 100644 day6/src/main.zig diff --git a/day6/build.zig b/day6/build.zig new file mode 100644 index 0000000..1dab3cf --- /dev/null +++ b/day6/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("day6", "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/day6/input b/day6/input new file mode 100644 index 0000000..9eae546 --- /dev/null +++ b/day6/input @@ -0,0 +1 @@ +1,1,1,1,1,5,1,1,1,5,1,1,3,1,5,1,4,1,5,1,2,5,1,1,1,1,3,1,4,5,1,1,2,1,1,1,2,4,3,2,1,1,2,1,5,4,4,1,4,1,1,1,4,1,3,1,1,1,2,1,1,1,1,1,1,1,5,4,4,2,4,5,2,1,5,3,1,3,3,1,1,5,4,1,1,3,5,1,1,1,4,4,2,4,1,1,4,1,1,2,1,1,1,2,1,5,2,5,1,1,1,4,1,2,1,1,1,2,2,1,3,1,4,4,1,1,3,1,4,1,1,1,2,5,5,1,4,1,4,4,1,4,1,2,4,1,1,4,1,3,4,4,1,1,5,3,1,1,5,1,3,4,2,1,3,1,3,1,1,1,1,1,1,1,1,1,4,5,1,1,1,1,3,1,1,5,1,1,4,1,1,3,1,1,5,2,1,4,4,1,4,1,2,1,1,1,1,2,1,4,1,1,2,5,1,4,4,1,1,1,4,1,1,1,5,3,1,4,1,4,1,1,3,5,3,5,5,5,1,5,1,1,1,1,1,1,1,1,2,3,3,3,3,4,2,1,1,4,5,3,1,1,5,5,1,1,2,1,4,1,3,5,1,1,1,5,2,2,1,4,2,1,1,4,1,3,1,1,1,3,1,5,1,5,1,1,4,1,2,1 diff --git a/day6/src/main.zig b/day6/src/main.zig new file mode 100644 index 0000000..24e8ebe --- /dev/null +++ b/day6/src/main.zig @@ -0,0 +1,100 @@ +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); + var it = std.mem.tokenize(contents, ","); + + // not unsurprisingly, tracking all the fish individually + // runs out of memory. maybe not? I might have misunderstood + // an error regarding parsing of a integer with a string + // that has a newline in it. + // good thing I abandoned this approach, since part 2 + // clearly is larger than the memory I have available. + // var fish = std.ArrayList(i8).init(alloc); + // defer fish.deinit(); + + // while (it.next()) |fi| { + // try fish.append(try std.fmt.parseInt(i8, fi, 10)); + // } + + // var n_cycles: u8 = 0; + // while (n_cycles < 80) : (n_cycles += 1) { + // var i: usize = 0; + // var len = fish.items.len; + // while (i < len) { + // fish.items[i] -= 1; + // if (fish.items[i] < 0) { + // fish.items[i] = 6; + // try fish.append(8); + // } + // } + // } + + // std.log.info("[Part 1] After {} cycles, there are {} fish", + // .{n_cycles, fish.items.len}); + + var days = [_] u64 { + 0, // 0: ready to spawn + 0, // 1: day 0 + 0, // ... + 0, + 0, + 0, + 0, + 0, // 7: day 6 + 0, + 0, // 9: day 8 + }; + while (it.next()) |fi| { + var val = try std.fmt.parseInt(u64, fi[0..1], 10); + days[val+1] += 1; // our index is val+1, since + // we have slot 0 storing currently reproducing + // fish + } + + var n_cycles: u16 = 0; + while (n_cycles < 80) : (n_cycles += 1) { + var i: usize = 1; + while (i < days.len) : (i += 1) { + // Move all fish from i to i-1 + days[i-1] = days[i]; + } + // Add new fish to days[9] + days[9] = days[0]; + // Move fish from days[0] to days[7] + days[7] += days[0]; + days[0] = 0; + } + var sum: u64 = 0; + for (days) |v| { + sum += v; + } + std.log.info("[Part 1] After {} cycles, there are {} fish", + .{n_cycles, sum}); + while (n_cycles < 256) : (n_cycles += 1) { + var i: usize = 1; + while (i < days.len) : (i += 1) { + // Move all fish from i to i-1 + days[i-1] = days[i]; + } + // Add new fish to days[9] + days[9] = days[0]; + // Move fish from days[0] to days[7] + days[7] += days[0]; + days[0] = 0; + } + sum = 0; + for (days) |v| { + sum += v; + } + std.log.info("[Part 2] After {} cycles, there are {} fish", + .{n_cycles, sum}); +}