aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/aoc.zig122
-rw-r--r--lib/console8.zig91
-rw-r--r--lib/input.zig1
3 files changed, 0 insertions, 214 deletions
diff --git a/lib/aoc.zig b/lib/aoc.zig
deleted file mode 100644
index ea373a5..0000000
--- a/lib/aoc.zig
+++ /dev/null
@@ -1,122 +0,0 @@
-const std = @import("std");
-pub const input = @import("input.zig");
-
-pub const Error = error{InvalidInput};
-
-pub var debug = false;
-pub var debuglvl: u32 = 0;
-
-const part_type = fn (alloc: *std.mem.Allocator, input: []u8, args: [][]u8) anyerror!void;
-pub fn gen_main(comptime part1: part_type, comptime part2: part_type) fn () anyerror!void {
- const impl = struct {
- fn main() !void {
- // create a default allocator
- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
- defer _ = gpa.deinit();
- var heapalloc = &gpa.allocator;
-
- // parse args
- const args = try std.process.argsAlloc(heapalloc);
- defer heapalloc.free(args);
- if (args.len < 2) return;
- const part = try std.fmt.parseInt(u8, args[1], 10);
-
- var filename: []const u8 = std.mem.spanZ("input");
- for (std.os.environ) |v| {
- const kv = std.mem.spanZ(v);
- if (std.mem.indexOfScalar(u8, kv, '=')) |sep| {
- if (sep == kv.len - 1) continue;
- if (std.mem.eql(u8, kv[0..sep], "AOCINPUT")) {
- filename = kv[sep + 1 ..];
- std.debug.print("Using input file: {}\n", .{filename});
- break;
- } else if (std.mem.eql(u8, kv[0..sep], "AOCDEBUG")) {
- debug = true;
- debuglvl = try std.fmt.parseInt(u32, kv[sep + 1 ..], 10);
- }
- }
- }
-
- // read all input into mem (files are always small so no problem)
- const file = try std.fs.cwd().openFile(filename, .{});
- const text = try file.reader().readAllAlloc(heapalloc, std.math.maxInt(u32));
- defer heapalloc.free(text);
-
- // exec part
- try switch (part) {
- 1 => part1(heapalloc, text, args[2..]),
- 2 => part2(heapalloc, text, args[2..]),
- else => std.debug.print("Invalid part number!\n", .{}),
- };
- }
- };
- return impl.main;
-}
-
-pub const Pos = struct {
- x: i64,
- y: i64,
- const Self = @This();
-
- pub fn add(self: Self, other: Self) Self {
- return Self{ .x = self.x + other.x, .y = self.y + other.y };
- }
-
- pub fn mult(self: Self, val: i64) Self {
- return Self{ .x = self.x * val, .y = self.y * val };
- }
-};
-
-pub const Dir = struct {
- pub const East = Pos{ .x = 1, .y = 0 };
- pub const West = Pos{ .x = -1, .y = 0 };
- pub const South = Pos{ .x = 0, .y = -1 };
- pub const North = Pos{ .x = 0, .y = 1 };
-
- pub const Name = enum {
- NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3
- };
- pub const dirs = [_]Pos{ North, East, South, West };
-
- pub fn get(name: Name) Pos {
- return dirs[@enumToInt(name)];
- }
-
- pub fn nextCW(dir: usize, offset: usize) usize {
- return (dir + @intCast(u32, offset)) % @intCast(u32, dirs.len);
- }
-
- pub fn nextCCW(dir: usize, offset: usize) usize {
- const constrained = offset % dirs.len;
- if (dir >= constrained) {
- return dir - constrained;
- } else {
- return dirs.len - (constrained - dir);
- }
- }
-
- const cos90vs = [_]i32{ 1, 0, -1, 0 };
- const sin90vs = [_]i32{ 0, 1, 0, -1 };
-
- pub fn rotCW(pos: Pos, offset: usize) Pos {
- const constrained = (4 - offset % 4) % 4;
- return Pos{
- .x = cos90vs[constrained] * pos.x - sin90vs[constrained] * pos.y,
- .y = sin90vs[constrained] * pos.x + cos90vs[constrained] * pos.y,
- };
- }
-
- pub fn rotCCW(pos: Pos, offset: usize) Pos {
- const constrained = offset % 4;
- std.debug.print("{}\n", .{constrained});
- return Pos{
- .x = cos90vs[constrained] * pos.x - sin90vs[constrained] * pos.y,
- .y = sin90vs[constrained] * pos.x + cos90vs[constrained] * pos.y,
- };
- }
-};
-
-pub fn assertV(v: anytype) !@TypeOf(v.?) {
- if (v == null) return Error.InvalidInput;
- return v.?;
-}
diff --git a/lib/console8.zig b/lib/console8.zig
deleted file mode 100644
index cfd8266..0000000
--- a/lib/console8.zig
+++ /dev/null
@@ -1,91 +0,0 @@
-const std = @import("std");
-
-pub const OpError = error{ InstructionPointerOOB, InvalidFormat, InstructionUnknown };
-pub const OpFuncSig = fn (ctx: *Console, arg: i64) OpError!void;
-pub const Instruction = struct {
- opcode: []const u8, opfunc: OpFuncSig, argval: i64
-};
-
-pub const Console = struct {
- accumulator: i64 = 0,
- instructptr: u64 = 0,
-
- jumpAddr: i65 = 0,
-
- code: []const u8,
- instlist: [][]const u8,
- allocator: *std.mem.Allocator,
- const Self = @This();
-
- pub fn init(code: []const u8, allocator: *std.mem.Allocator) !Self {
- var instvec = std.ArrayList([]const u8).init(allocator);
- errdefer instvec.deinit();
-
- var instit = std.mem.tokenize(code, "\n");
- while (instit.next()) |inst| {
- try instvec.append(inst);
- }
- return Console{
- .code = code,
- .instlist = instvec.toOwnedSlice(),
- .allocator = allocator,
- };
- }
-
- pub fn deinit(self: *Self) void {
- self.allocator.free(self.instlist);
- }
-
- pub fn reset(self: *Self) void {
- self.accumulator = 0;
- self.instructptr = 0;
- }
-
- const instructionMap = std.ComptimeStringMap(OpFuncSig, .{
- .{ "jmp", jumpInstruction },
- .{ "acc", accInstruction },
- .{ "nop", nopInstruction },
- });
-
- pub fn jumpInstruction(self: *Self, arg: i64) OpError!void {
- self.jumpAddr = @intCast(i65, self.instructptr) + @intCast(i65, arg);
- if (self.jumpAddr < 0 or self.jumpAddr >= self.instlist.len)
- return error.InstructionPointerOOB;
- self.instructptr = @intCast(u64, self.jumpAddr);
- }
-
- pub fn accInstruction(self: *Self, arg: i64) OpError!void {
- self.accumulator += arg;
- self.instructptr += 1;
- }
-
- pub fn nopInstruction(self: *Self, arg: i64) OpError!void {
- self.instructptr += 1;
- }
-
- pub fn parseNext(self: *Self) !?Instruction {
- if (self.instructptr >= self.instlist.len)
- return null;
-
- const inststr = self.instlist[self.instructptr];
- const sep = std.mem.indexOfScalar(u8, inststr, ' ');
- if (sep == null) return OpError.InvalidFormat;
-
- const opcode = inststr[0..sep.?];
- if (instructionMap.get(opcode)) |opfunc| {
- const arg = inststr[sep.? + 1 ..];
- const val = std.fmt.parseInt(i64, arg, 10) catch |err| {
- std.debug.print("Failed to parse arg value: {}\n", .{arg});
- return OpError.InvalidFormat;
- };
- return Instruction{ .opcode = opcode, .opfunc = opfunc, .argval = val };
- } else {
- std.debug.print("Unknown instruction: {}\n", .{inststr});
- return OpError.InstructionUnknown;
- }
- }
-
- pub fn exec(self: *Self, inst: *Instruction) !void {
- try inst.opfunc(self, inst.argval);
- }
-};
diff --git a/lib/input.zig b/lib/input.zig
deleted file mode 100644
index 95a0b68..0000000
--- a/lib/input.zig
+++ /dev/null
@@ -1 +0,0 @@
-const std = @import("std");