init
This commit is contained in:
commit
a02a9713ad
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
zig-cache/
|
||||
zig-out/
|
||||
deps.zig
|
||||
gyro.lock
|
||||
.gyro
|
34
build.zig
Normal file
34
build.zig
Normal file
|
@ -0,0 +1,34 @@
|
|||
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("aoc2022", "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);
|
||||
|
||||
const exe_tests = b.addTest("src/main.zig");
|
||||
exe_tests.setTarget(target);
|
||||
exe_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&exe_tests.step);
|
||||
}
|
2237
input/day1.txt
Normal file
2237
input/day1.txt
Normal file
File diff suppressed because it is too large
Load diff
4
src/aoc.zig
Normal file
4
src/aoc.zig
Normal file
|
@ -0,0 +1,4 @@
|
|||
pub const DayInfo = struct {
|
||||
num: usize,
|
||||
name: []const u8,
|
||||
};
|
57
src/days/day1.zig
Normal file
57
src/days/day1.zig
Normal file
|
@ -0,0 +1,57 @@
|
|||
const std = @import("std");
|
||||
const aoc = @import("../aoc.zig");
|
||||
|
||||
pub const info = aoc.DayInfo{
|
||||
.num = 1,
|
||||
.name = "Calorie Counting",
|
||||
};
|
||||
|
||||
fn getCounts(alloc: std.mem.Allocator, infile: *std.fs.File) !std.ArrayList(usize) {
|
||||
var sums = std.ArrayList(usize).init(alloc);
|
||||
errdefer sums.deinit();
|
||||
|
||||
var reader = std.io.bufferedReader(infile.reader());
|
||||
|
||||
var cur_idx: usize = 0;
|
||||
try sums.append(0);
|
||||
|
||||
var line_buf: [512]u8 = undefined;
|
||||
while (try reader.reader().readUntilDelimiterOrEof(&line_buf, '\n')) |line| {
|
||||
const trimmed = std.mem.trim(u8, line, &std.ascii.whitespace);
|
||||
|
||||
if (trimmed.len == 0) {
|
||||
try sums.append(0);
|
||||
cur_idx += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
sums.items[cur_idx] += try std.fmt.parseInt(usize, trimmed, 10);
|
||||
}
|
||||
|
||||
return sums;
|
||||
}
|
||||
|
||||
pub fn part1(alloc: std.mem.Allocator, infile: *std.fs.File) !void {
|
||||
const sums = try getCounts(alloc, infile);
|
||||
defer sums.deinit();
|
||||
|
||||
try std.io.getStdOut().writer().print(
|
||||
"{d}\n",
|
||||
.{std.mem.max(usize, sums.items)},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn part2(alloc: std.mem.Allocator, infile: *std.fs.File) !void {
|
||||
var sums = try getCounts(alloc, infile);
|
||||
defer sums.deinit();
|
||||
|
||||
var sum: usize = 0;
|
||||
var i: usize = 0;
|
||||
while (i < 3) : (i += 1) {
|
||||
const max_idx = std.mem.indexOfMax(usize, sums.items);
|
||||
sum += sums.items[max_idx];
|
||||
sums.items[max_idx] = 0;
|
||||
}
|
||||
|
||||
try std.io.getStdOut().writer().print("{d}\n", .{sum});
|
||||
}
|
37
src/main.zig
Normal file
37
src/main.zig
Normal file
|
@ -0,0 +1,37 @@
|
|||
const std = @import("std");
|
||||
const aoc = @import("aoc.zig");
|
||||
|
||||
const days = .{
|
||||
@import("days/day1.zig"),
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const alloc = gpa.allocator();
|
||||
|
||||
inline for (days) |day| {
|
||||
const info: aoc.DayInfo = day.info;
|
||||
|
||||
const input_path = try std.fmt.allocPrint(alloc, "input/day{}.txt", .{info.num});
|
||||
defer alloc.free(input_path);
|
||||
|
||||
var input_file = try std.fs.cwd().openFile(input_path, .{});
|
||||
defer input_file.close();
|
||||
|
||||
try std.io.getStdOut().writer().print(
|
||||
"\n==== Day {}: {s} (Part 1) ====\n",
|
||||
.{ info.num, info.name },
|
||||
);
|
||||
try day.part1(alloc, &input_file);
|
||||
|
||||
if (@hasDecl(day, "part2")) {
|
||||
try input_file.seekTo(0);
|
||||
try std.io.getStdOut().writer().print(
|
||||
"\n==== Day {}: {s} (Part 2) ====\n",
|
||||
.{ info.num, info.name },
|
||||
);
|
||||
try day.part2(alloc, &input_file);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue