Compare commits

..

No commits in common. "794cc70d602bb22e9b0d1c9cc2e2dcc6d5c5a667" and "5499514ab0e414645f0a4288c8bf7f6823773d42" have entirely different histories.

5 changed files with 1 additions and 1089 deletions

View File

@ -5,13 +5,12 @@ pub fn main() anyerror!void {
defer arena.deinit();
const alloc = &arena.allocator;
// Read our input
// 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);
// Part 1
var depths = std.ArrayList(u32).init(alloc);
defer depths.deinit();

View File

@ -1,27 +0,0 @@
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);
}

1000
day2/input

File diff suppressed because it is too large Load Diff

View File

@ -1,2 +0,0 @@
info: [Part 1] Depth 916, Horizontal Position 1845, Sum 1690020
info: [Part 2] Depth 763408, Horizontal Position 1845, Sum 1408487760

View File

@ -1,58 +0,0 @@
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});
}