Day 1 done, needs some polishing
commit
ae8b29d9f8
|
@ -0,0 +1,2 @@
|
||||||
|
zig-cache/*
|
||||||
|
zig-out/*
|
|
@ -0,0 +1,36 @@
|
||||||
|
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 dayPath = "src/day1.zig";
|
||||||
|
|
||||||
|
const exe = b.addExecutable("advent_of_code", dayPath);
|
||||||
|
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(dayPath);
|
||||||
|
exe_tests.setTarget(target);
|
||||||
|
exe_tests.setBuildMode(mode);
|
||||||
|
|
||||||
|
const test_step = b.step("test", "Run unit tests");
|
||||||
|
test_step.dependOn(&exe_tests.step);
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,45 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
var file = try std.fs.cwd().openFile("inputs/day1.txt", .{});
|
||||||
|
defer file.close();
|
||||||
|
|
||||||
|
var buf_reader = std.io.bufferedReader(file.reader());
|
||||||
|
var in_stream = buf_reader.reader();
|
||||||
|
|
||||||
|
var buf: [1024]u8 = undefined;
|
||||||
|
var elf_sums: [1024]i32 = [_]i32{0} ** 1024;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
|
||||||
|
if (line.len == 0) {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const calorie_count = std.fmt.parseInt(i32, line, 10) catch |err| {
|
||||||
|
std.debug.print("Error converting str: {?}\n", .{err});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
elf_sums[i] += calorie_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std.sort.sort(i32, &elf_sums, {}, std.sort.desc(i32));
|
||||||
|
|
||||||
|
var sum: i32 = 0;
|
||||||
|
for (elf_sums[0..3]) |value, index| {
|
||||||
|
std.debug.print("Pos {} = {}\n", .{index+1, value});
|
||||||
|
sum += value;
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("Total calories of the top three elves: {}\n", .{sum});
|
||||||
|
}
|
||||||
|
|
||||||
|
test "simple test" {
|
||||||
|
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||||
|
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
||||||
|
try list.append(42);
|
||||||
|
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
||||||
|
}
|
Loading…
Reference in New Issue