Finish day 2
This commit is contained in:
parent
5499514ab0
commit
859699bcd0
|
@ -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);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,2 @@
|
|||
info: [Part 1] Depth 916, Horizontal Position 1845, Sum 1690020
|
||||
info: [Part 2] Depth 763408, Horizontal Position 1845, Sum 1408487760
|
|
@ -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});
|
||||
}
|
Reference in New Issue