Initial commit: Day 1
This commit is contained in:
commit
5499514ab0
|
@ -0,0 +1,2 @@
|
|||
zig-cache/
|
||||
zig-out/
|
|
@ -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("day1", "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);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,2 @@
|
|||
info: [Part 1] There were 1521 increases
|
||||
info: [Part 2] There were 1543 increases
|
|
@ -0,0 +1,53 @@
|
|||
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;
|
||||
|
||||
// Part 1
|
||||
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 depths = std.ArrayList(u32).init(alloc);
|
||||
defer depths.deinit();
|
||||
|
||||
var it = std.mem.tokenize(contents, "\n");
|
||||
var max: u32 = 0;
|
||||
var increases: u32 = 0;
|
||||
while (it.next()) |line| {
|
||||
var v = try std.fmt.parseInt(u32, line, 10);
|
||||
// This will always happen
|
||||
defer max = v;
|
||||
// Fill in the depths for part2
|
||||
try depths.append(v);
|
||||
if (max == 0) {
|
||||
continue;
|
||||
}
|
||||
if (v > max) {
|
||||
increases += 1;
|
||||
}
|
||||
}
|
||||
std.log.info("[Part 1] There were {} increases", .{increases});
|
||||
|
||||
// Part 2
|
||||
// Working with a sliding window of 3
|
||||
var i: usize = 0;
|
||||
increases = 0;
|
||||
max = 0;
|
||||
while (i < depths.items.len - 3) : (i += 1) {
|
||||
// Current window
|
||||
var a: u32 = depths.items[i] + depths.items[i+1] + depths.items[i+2];
|
||||
var b: u32 = depths.items[i+1] + depths.items[i+2] + depths.items[i+3];
|
||||
// Visualize it
|
||||
//std.log.info("A: {} // B: {}\n{} A\n{} A B\n{} A B\n{} B\n",
|
||||
// .{a, b, depths.items[i], depths.items[i+1],
|
||||
// depths.items[i+2], depths.items[i+3]});
|
||||
if (b > a) {
|
||||
increases += 1;
|
||||
}
|
||||
}
|
||||
std.log.info("[Part 2] There were {} increases", .{increases});
|
||||
}
|
Loading…
Reference in New Issue