Day 8, all in

This commit is contained in:
2019-12-08 13:12:09 +00:00
parent 9864e914ba
commit 96e35f265e
3 changed files with 119 additions and 0 deletions

14
08/build.zig Normal file
View File

@@ -0,0 +1,14 @@
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("08", "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);
}

1
08/input Normal file

File diff suppressed because one or more lines are too long

104
08/src/main.zig Normal file
View File

@@ -0,0 +1,104 @@
const std = @import("std");
const depth = 100;
const width = 25;
const height = 6;
const Layer = [height][width]Colour;
const Image = [depth]Layer;
const Colour = enum(u8) {
Black = 0,
White = 1,
Trans = 2,
};
fn printLayer(l: Layer) void {
for (l) |row| {
for (row) |byte| {
switch (byte) {
.Black => {
std.debug.warn(" ");
},
.White => {
std.debug.warn("*");
},
else => {
std.debug.warn(" ");
},
}
}
std.debug.warn("\n");
}
std.debug.warn("\n");
}
fn printImage(image: Image) void {
var meta: Layer = undefined;
for (meta) |row, y| {
for (row) |_, x| {
meta[y][x] = .Trans;
}
}
for (meta) |*row, y| {
for (row) |*b, x| {
var z: usize = 0;
while (b.* == .Trans) : (z += 1) {
if (image[z][y][x] != .Trans) {
b.* = image[z][y][x];
}
}
}
}
printLayer(meta);
}
pub fn main() anyerror!void {
var img: Image = undefined;
var zeroCounts = [_]usize{0} ** depth;
const file = std.io.getStdIn();
const stream = &file.inStream().stream;
for (img) |*layer, z| {
for (layer) |*row| {
for (row) |*pixel| {
var b = try stream.readByte();
if (b == '0') zeroCounts[z] += 1;
pixel.* = @intToEnum(Colour, b - '0');
}
}
}
// Now count how many zeroes are in each layer, picking out the smallest
var minCount: usize = width * height;
var minZ: usize = 0;
for (zeroCounts) |count, i| {
if (count < minCount) {
minCount = count;
minZ = i;
}
}
var num1s: usize = 0;
var num2s: usize = 0;
for (img[minZ]) |row| {
for (row) |b| {
switch (b) {
.White => {
num1s += 1;
},
.Trans => {
num2s += 1;
},
else => {},
}
}
}
std.debug.warn("Day 8, Part 1: {} with {} zeroes, {} 1s, {} 2s. Result: {}\n\n", minZ, minCount, num1s, num2s, num1s * num2s);
printImage(img);
}