zig aoc
This commit is contained in:
commit
4671b7691c
|
|
@ -0,0 +1 @@
|
|||
.zig-cache
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "day1",
|
||||
.root_module = b.createModule(.{
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
.root_source_file = b.path("day1.zig"),
|
||||
}),
|
||||
});
|
||||
|
||||
b.installArtifact(exe);
|
||||
const run = b.addRunArtifact(exe);
|
||||
const step = b.step("day1", "run day");
|
||||
step.dependOn(&run.step);
|
||||
|
||||
if (b.args) |args| {
|
||||
run.addArgs(args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const shared = @import("shared.zig");
|
||||
const print = shared.print;
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
try shared.sharedInit(init);
|
||||
const allocator: std.mem.Allocator = init.arena.allocator();
|
||||
const bytes = shared.loadBytesArg(allocator);
|
||||
|
||||
var l: shared.LineIter = .{};
|
||||
var acc: i64 = 50;
|
||||
var part1: i64 = 0;
|
||||
var part2: i64 = 0;
|
||||
while (l.nextLine(bytes)) |line| {
|
||||
const c = line[0];
|
||||
const r = std.fmt.parseInt(i64, line[1..line.len], 10) catch unreachable;
|
||||
if (c == 'R') {
|
||||
if (acc < 0 and acc + r >= 0)
|
||||
part2 += 1;
|
||||
|
||||
acc += r;
|
||||
while (acc >= 100) {
|
||||
acc -= 100;
|
||||
part2 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == 'L') {
|
||||
if (acc > 0 and acc - r <= 0)
|
||||
part2 += 1;
|
||||
|
||||
acc -= r;
|
||||
while (acc <= -100) {
|
||||
acc += 100;
|
||||
part2 += 1;
|
||||
}
|
||||
|
||||
if (acc < 0) acc += 100;
|
||||
}
|
||||
|
||||
if (acc == 0) part1 += 1;
|
||||
const x: u8 = if (c == 'R') '+' else '-';
|
||||
print("{d}: '{s}' {d} {c} part1={d} acc={d} part2={d}", .{ l.line, line, r, x, part1, acc, part2 });
|
||||
}
|
||||
|
||||
print("answers: {d} {d}", .{ part1, part2 });
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,10 @@
|
|||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
var stdout_buffer: [1024]u8 = undefined;
|
||||
var stdout_file_writer: Io.File.Writer = undefined;
|
||||
var process: std.process.Init = undefined;
|
||||
|
||||
pub fn print(comptime fmt: []const u8, args: anytype) void {
|
||||
writer().print(fmt ++ "\n", args) catch unreachable;
|
||||
writer().flush() catch unreachable;
|
||||
}
|
||||
|
||||
pub fn writer() *Io.Writer {
|
||||
return &stdout_file_writer.interface;
|
||||
}
|
||||
|
||||
pub fn sharedInit(init: std.process.Init) !void {
|
||||
process = init;
|
||||
const io = init.io;
|
||||
stdout_file_writer = .init(.stdout(), io, &stdout_buffer);
|
||||
}
|
||||
|
||||
pub fn loadBytesFile(allocator: std.mem.Allocator, path: []const u8) []u8 {
|
||||
const bytes = std.Io.Dir.cwd().readFileAlloc(process.io, path, allocator, .unlimited) catch {
|
||||
std.debug.print("unable to load file {s}\n", .{path});
|
||||
unreachable;
|
||||
};
|
||||
return bytes;
|
||||
}
|
||||
|
||||
pub fn loadBytesArg(allocator: std.mem.Allocator) []const u8 {
|
||||
// Accessing command line arguments:
|
||||
const args = process.minimal.args.toSlice(allocator) catch unreachable;
|
||||
|
||||
if (args.len < 2) {
|
||||
print("invalid number of arguments", .{});
|
||||
unreachable;
|
||||
}
|
||||
return trim(normalizeEndings(allocator, loadBytesFile(allocator, args[1])));
|
||||
}
|
||||
|
||||
pub fn normalizeEndings(allocator: std.mem.Allocator, bytes: []const u8) []u8 {
|
||||
var s: std.ArrayList(u8) = .empty;
|
||||
|
||||
for (bytes, 0..) |c, i| {
|
||||
if (c != '\r' or c != '\n')
|
||||
s.append(allocator, c) catch unreachable;
|
||||
if (c == '\r') if (bytes[i + 1] == '\n')
|
||||
s.append(allocator, '\n') catch unreachable;
|
||||
}
|
||||
|
||||
return s.toOwnedSlice(allocator) catch unreachable;
|
||||
}
|
||||
|
||||
pub fn isWhiteSpace(s: []const u8, off: usize) bool {
|
||||
const c = s[off];
|
||||
return c == ' ' or c == '\n' or c == '\r' or c == '\t';
|
||||
}
|
||||
|
||||
pub fn trim(bytes: []const u8) []const u8 {
|
||||
if (bytes.len == 0) return bytes;
|
||||
|
||||
var start: usize = 0;
|
||||
while (isWhiteSpace(bytes, start)) start += 1;
|
||||
|
||||
var end: usize = bytes.len - 1;
|
||||
while (end - 1 > 0 and isWhiteSpace(bytes, end)) end -= 1;
|
||||
|
||||
return bytes[start .. end + 1];
|
||||
}
|
||||
|
||||
pub const LineIter = struct {
|
||||
line: usize = 0,
|
||||
s: usize = 0,
|
||||
e: usize = 0,
|
||||
|
||||
pub fn toSlice(self: *@This(), bytes: []const u8) []const u8 {
|
||||
return trim(bytes[self.s..self.e]);
|
||||
}
|
||||
|
||||
pub fn nextLine(self: *@This(), bytes: []const u8) ?[]const u8 {
|
||||
if (self.e == bytes.len) {
|
||||
return null;
|
||||
}
|
||||
|
||||
self.s = self.e;
|
||||
if (isWhiteSpace(bytes, self.e)) self.s += 1;
|
||||
|
||||
self.e = self.s;
|
||||
self.line += 1;
|
||||
|
||||
while (self.e != bytes.len and bytes[self.e] != '\n') {
|
||||
self.e += 1;
|
||||
}
|
||||
|
||||
if (self.e != bytes.len and bytes[self.e] == '\n') self.e += 1;
|
||||
|
||||
return self.toSlice(bytes);
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue