diff options
-rw-r--r-- | day-1/main.zig | 39 | ||||
-rw-r--r-- | day-1/main_1.zig | 34 | ||||
-rw-r--r-- | day-2/main.zig | 122 | ||||
-rw-r--r-- | day-2/main_1.zig | 68 | ||||
l--------- | day-2/util | 1 | ||||
-rw-r--r-- | day-2/util/file.zig | 15 | ||||
-rw-r--r-- | day-2/util/mem.zig | 17 | ||||
-rw-r--r-- | day-3/main.zig | 37 | ||||
-rw-r--r-- | day-3/main_1.zig | 39 | ||||
-rw-r--r-- | main.zig | 70 |
10 files changed, 209 insertions, 233 deletions
diff --git a/day-1/main.zig b/day-1/main.zig index fa89e3f..0af8162 100644 --- a/day-1/main.zig +++ b/day-1/main.zig @@ -1,14 +1,8 @@ const std = @import("std"); -const slurp = @import("util/file.zig").slurp; +const min_idx = @import("util/mem.zig").min_idx; -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - -pub fn main() !void { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); - - var iter = std.mem.split(u8, file_buffer, "\n"); +pub fn puzzle_1(input: []u8) !i32 { + var iter = std.mem.split(u8, input, "\n"); var count: i32 = 0; var max: i32 = 0; @@ -23,5 +17,30 @@ pub fn main() !void { } } - std.debug.print("{d}\n", .{max}); + return max; +} + +pub fn puzzle_2(input: []u8) !i32 { + var iter = std.mem.split(u8, input, "\n"); + var count: i32 = 0; + var max: [3]i32 = std.mem.zeroes([3]i32); + + while (iter.next()) |line| { + if (line.len == 0) { + const lowest_u = min_idx(i32, &max); + if (count > max[lowest_u]) { + max[lowest_u] = count; + } + count = 0; + } else { + count += try std.fmt.parseInt(i32, line, 0); + } + } + + count = 0; + for (max) |v| { + count += v; + } + + return count; } diff --git a/day-1/main_1.zig b/day-1/main_1.zig deleted file mode 100644 index 5e502d3..0000000 --- a/day-1/main_1.zig +++ /dev/null @@ -1,34 +0,0 @@ -const std = @import("std"); -const slurp = @import("util/file.zig").slurp; -const min_idx = @import("util/mem.zig").min_idx; - -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - -pub fn main() !void { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); - - var iter = std.mem.split(u8, file_buffer, "\n"); - var count: i32 = 0; - var max: [3]i32 = std.mem.zeroes([3]i32); - - while (iter.next()) |line| { - if (line.len == 0) { - const lowest_u = min_idx(i32, &max); - if (count > max[lowest_u]) { - max[lowest_u] = count; - } - count = 0; - } else { - count += try std.fmt.parseInt(i32, line, 0); - } - } - - count = 0; - for (max) |v| { - count += v; - } - - std.debug.print("{d}\n", .{count}); -} diff --git a/day-2/main.zig b/day-2/main.zig index 4053caa..f493580 100644 --- a/day-2/main.zig +++ b/day-2/main.zig @@ -1,68 +1,110 @@ const std = @import("std"); const slurp = @import("util/file.zig").slurp; -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - const Hand = enum(u8) { Rock = 1, Paper = 2, Scissors = 3 }; const Outcome = enum(u8) { Win = 6, Draw = 3, Lose = 0 }; -const KV = struct { @"0": []const u8, @"1": Hand }; +const KV_Hand = struct { @"0": []const u8, @"1": Hand }; +const KV_Outcome = struct { @"0": []const u8, @"1": Outcome }; -const Player_A_KV = std.ComptimeStringMap(Hand, [_]KV{ +const Player_A_KV = std.ComptimeStringMap(Hand, [_]KV_Hand{ .{ .@"0" = "A", .@"1" = .Rock }, .{ .@"0" = "B", .@"1" = .Paper }, .{ .@"0" = "C", .@"1" = .Scissors }, }); -const Player_B_KV = std.ComptimeStringMap(Hand, [_]KV{ +const Player_B_KV = std.ComptimeStringMap(Hand, [_]KV_Hand{ .{ .@"0" = "X", .@"1" = .Rock }, .{ .@"0" = "Y", .@"1" = .Paper }, .{ .@"0" = "Z", .@"1" = .Scissors }, }); -pub fn main() !void { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); +const Outcome_KV = std.ComptimeStringMap(Outcome, [_]KV_Outcome{ + .{ .@"0" = "X", .@"1" = .Lose }, + .{ .@"0" = "Y", .@"1" = .Draw }, + .{ .@"0" = "Z", .@"1" = .Win }, +}); + +const match_impl = struct { + fn puzzle_1(a: Hand, b: Hand) u8 { + switch (b) { + .Rock => { + switch (a) { + .Rock => return @enumToInt(b) + @enumToInt(Outcome.Draw), + .Paper => return @enumToInt(b) + @enumToInt(Outcome.Lose), + .Scissors => return @enumToInt(b) + @enumToInt(Outcome.Win), + } + }, + .Paper => { + switch (a) { + .Rock => return @enumToInt(b) + @enumToInt(Outcome.Win), + .Paper => return @enumToInt(b) + @enumToInt(Outcome.Draw), + .Scissors => return @enumToInt(b) + @enumToInt(Outcome.Lose), + } + }, + .Scissors => { + switch (a) { + .Rock => return @enumToInt(b) + @enumToInt(Outcome.Lose), + .Paper => return @enumToInt(b) + @enumToInt(Outcome.Win), + .Scissors => return @enumToInt(b) + @enumToInt(Outcome.Draw), + } + }, + } + } + + fn puzzle_2(a: Hand, b: Outcome) u8 { + switch (a) { + .Rock => { + switch (b) { + .Win => return @enumToInt(b) + @enumToInt(Hand.Paper), + .Lose => return @enumToInt(b) + @enumToInt(Hand.Scissors), + .Draw => return @enumToInt(b) + @enumToInt(Hand.Rock), + } + }, + .Paper => { + switch (b) { + .Win => return @enumToInt(b) + @enumToInt(Hand.Scissors), + .Lose => return @enumToInt(b) + @enumToInt(Hand.Rock), + .Draw => return @enumToInt(b) + @enumToInt(Hand.Paper), + } + }, + .Scissors => { + switch (b) { + .Win => return @enumToInt(b) + @enumToInt(Hand.Rock), + .Lose => return @enumToInt(b) + @enumToInt(Hand.Paper), + .Draw => return @enumToInt(b) + @enumToInt(Hand.Scissors), + } + }, + } + } +}; - var iter = std.mem.split(u8, file_buffer, "\n"); +pub fn puzzle_1(input: []u8) u16 { + var iter = std.mem.split(u8, input, "\n"); var score: u16 = 0; while (iter.next()) |line| { - if (line.len > 0) { - score += match( - Player_A_KV.get(&[_]u8{line[0]}).?, - Player_B_KV.get(&[_]u8{line[2]}).?, - ); - } + score += match_impl.puzzle_1( + Player_A_KV.get(&[_]u8{line[0]}).?, + Player_B_KV.get(&[_]u8{line[2]}).?, + ); } - std.debug.print("{d}\n", .{score}); + return score; } -fn match(a: Hand, b: Hand) u8 { - switch (b) { - .Rock => { - switch (a) { - .Rock => return @enumToInt(b) + @enumToInt(Outcome.Draw), - .Paper => return @enumToInt(b) + @enumToInt(Outcome.Lose), - .Scissors => return @enumToInt(b) + @enumToInt(Outcome.Win), - } - }, - .Paper => { - switch (a) { - .Rock => return @enumToInt(b) + @enumToInt(Outcome.Win), - .Paper => return @enumToInt(b) + @enumToInt(Outcome.Draw), - .Scissors => return @enumToInt(b) + @enumToInt(Outcome.Lose), - } - }, - .Scissors => { - switch (a) { - .Rock => return @enumToInt(b) + @enumToInt(Outcome.Lose), - .Paper => return @enumToInt(b) + @enumToInt(Outcome.Win), - .Scissors => return @enumToInt(b) + @enumToInt(Outcome.Draw), - } - }, +pub fn puzzle_2(input: []u8) u16 { + var iter = std.mem.split(u8, input, "\n"); + + var score: u16 = 0; + + while (iter.next()) |line| { + score += match_impl.puzzle_2( + Player_A_KV.get(&[_]u8{line[0]}).?, + Outcome_KV.get(&[_]u8{line[2]}).?, + ); } + + return score; } diff --git a/day-2/main_1.zig b/day-2/main_1.zig deleted file mode 100644 index 51d6b65..0000000 --- a/day-2/main_1.zig +++ /dev/null @@ -1,68 +0,0 @@ -const std = @import("std"); -const slurp = @import("util/file.zig").slurp; - -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - -const Hand = enum(u8) { Rock = 1, Paper = 2, Scissors = 3 }; -const Outcome = enum(u8) { Win = 6, Draw = 3, Lose = 0 }; - -const KV_Hand = struct { @"0": []const u8, @"1": Hand }; -const KV_Outcome = struct { @"0": []const u8, @"1": Outcome }; - -const Player_A_KV = std.ComptimeStringMap(Hand, [_]KV_Hand{ - .{ .@"0" = "A", .@"1" = .Rock }, - .{ .@"0" = "B", .@"1" = .Paper }, - .{ .@"0" = "C", .@"1" = .Scissors }, -}); - -const Outcome_KV = std.ComptimeStringMap(Outcome, [_]KV_Outcome{ - .{ .@"0" = "X", .@"1" = .Lose }, - .{ .@"0" = "Y", .@"1" = .Draw }, - .{ .@"0" = "Z", .@"1" = .Win }, -}); - -pub fn main() !void { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); - - var iter = std.mem.split(u8, file_buffer, "\n"); - - var score: u16 = 0; - - while (iter.next()) |line| { - if (line.len > 0) - score += match( - Player_A_KV.get(&[_]u8{line[0]}).?, - Outcome_KV.get(&[_]u8{line[2]}).?, - ); - } - - std.debug.print("{d}\n", .{score}); -} - -fn match(a: Hand, b: Outcome) u8 { - switch (a) { - .Rock => { - switch (b) { - .Win => return @enumToInt(b) + @enumToInt(Hand.Paper), - .Lose => return @enumToInt(b) + @enumToInt(Hand.Scissors), - .Draw => return @enumToInt(b) + @enumToInt(Hand.Rock), - } - }, - .Paper => { - switch (b) { - .Win => return @enumToInt(b) + @enumToInt(Hand.Scissors), - .Lose => return @enumToInt(b) + @enumToInt(Hand.Rock), - .Draw => return @enumToInt(b) + @enumToInt(Hand.Paper), - } - }, - .Scissors => { - switch (b) { - .Win => return @enumToInt(b) + @enumToInt(Hand.Rock), - .Lose => return @enumToInt(b) + @enumToInt(Hand.Paper), - .Draw => return @enumToInt(b) + @enumToInt(Hand.Scissors), - } - }, - } -} diff --git a/day-2/util b/day-2/util new file mode 120000 index 0000000..40c3fc5 --- /dev/null +++ b/day-2/util @@ -0,0 +1 @@ +../util
\ No newline at end of file diff --git a/day-2/util/file.zig b/day-2/util/file.zig deleted file mode 100644 index 90849e9..0000000 --- a/day-2/util/file.zig +++ /dev/null @@ -1,15 +0,0 @@ -const std = @import("std"); - -/// Reads an entire file into memory, caller owns the returned slice. -pub fn slurp(allocator: std.mem.Allocator, file_path: []const u8) ![]u8 { - var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const path = try std.fs.realpath(file_path, &path_buffer); - - const file = try std.fs.openFileAbsolute(path, .{}); - defer file.close(); - - return try file.readToEndAlloc( - allocator, - (try file.stat()).size, - ); -} diff --git a/day-2/util/mem.zig b/day-2/util/mem.zig deleted file mode 100644 index 89ba67b..0000000 --- a/day-2/util/mem.zig +++ /dev/null @@ -1,17 +0,0 @@ -const std = @import("std"); -const math = std.math; - -/// Returns the position of the smallest number in a slice. -pub fn min_idx(comptime T: type, slice: []const T) usize { - var best = slice[0]; - var idx: usize = 0; - - for (slice[1..]) |item, i| { - const possible_best = math.min(best, item); - if (best > possible_best) { - best = possible_best; - idx = i + 1; - } - } - return idx; -} diff --git a/day-3/main.zig b/day-3/main.zig index 4406a79..0bba2f3 100644 --- a/day-3/main.zig +++ b/day-3/main.zig @@ -1,15 +1,8 @@ const std = @import("std"); -const slurp = @import("util/file.zig").slurp; const dupl_values = @import("util/mem.zig").dupl_values; -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - -pub fn main() !void { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); - - var iter = std.mem.split(u8, file_buffer, "\n"); +pub fn puzzle_1(allocator: std.mem.Allocator, input: []u8) !u16 { + var iter = std.mem.split(u8, input, "\n"); var count: u16 = 0; while (iter.next()) |line| { @@ -28,7 +21,31 @@ pub fn main() !void { } } - std.debug.print("{d}\n", .{count}); + return count; +} + +pub fn puzzle_2(allocator: std.mem.Allocator, input: []u8) !u16 { + var iter = std.mem.split(u8, input, "\n"); + + var count: u16 = 0; + while (iter.next()) |line| { + const duplicates = try dupl_values( + u8, + allocator, + &[_][]const u8{ + line, + iter.next().?, + iter.next().?, + }, + ); + defer allocator.free(duplicates); + + for (duplicates) |char| { + count += char_to_priority(char); + } + } + + return count; } fn char_to_priority(char: u8) u8 { diff --git a/day-3/main_1.zig b/day-3/main_1.zig deleted file mode 100644 index d65161b..0000000 --- a/day-3/main_1.zig +++ /dev/null @@ -1,39 +0,0 @@ -const std = @import("std"); -const slurp = @import("util/file.zig").slurp; -const dupl_values = @import("util/mem.zig").dupl_values; - -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); - -pub fn main() !void { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); - - var iter = std.mem.split(u8, file_buffer, "\n"); - - var count: u16 = 0; - while (iter.next()) |line| { - const duplicates = try dupl_values( - u8, - allocator, - &[_][]const u8{ - line, - iter.next().?, - iter.next().?, - }, - ); - defer allocator.free(duplicates); - - for (duplicates) |char| { - count += char_to_priority(char); - } - } - - std.debug.print("{d}\n", .{count}); -} - -fn char_to_priority(char: u8) u8 { - if (char >= 65 and char <= 90) - return char - 38; - return char - 96; -} diff --git a/main.zig b/main.zig new file mode 100644 index 0000000..276e611 --- /dev/null +++ b/main.zig @@ -0,0 +1,70 @@ +const std = @import("std"); + +const t = [_]struct { + day: type, + expect: [2]i32, +}{ + .{ .day = @import("day-1/main.zig"), .expect = [_]i32{ 69912, 208180 } }, + .{ .day = @import("day-2/main.zig"), .expect = [_]i32{ 12855, 13726 } }, + .{ .day = @import("day-3/main.zig"), .expect = [_]i32{ 8039, 2510 } }, +}; + +test { + std.testing.refAllDecls(@This()); +} + +const allocator = std.testing.allocator; +const slurp = @import("util/file.zig").slurp; + +// just here to inline it, see: https://github.com/ziglang/zig/issues/12635 +inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [std.fmt.count(fmt, args):0]u8 { + comptime { + var buf: [std.fmt.count(fmt, args):0]u8 = undefined; + _ = std.fmt.bufPrint(&buf, fmt, args) catch unreachable; + buf[buf.len] = 0; + return &buf; + } +} + +test "test" { + std.debug.print("\n", .{}); + + inline for (t) |aoc_day, day| { + std.debug.print("\n", .{}); + const file_buffer = try slurp(std.testing.allocator, comptimePrint("./day-{d}/input", .{day + 1})); + defer allocator.free(file_buffer); + + inline for (aoc_day.expect) |expected, idx| { + const fn_name = comptimePrint("puzzle_{d}", .{idx + 1}); + + const puzzle = @field(aoc_day.day, fn_name); + const result = blk: { + switch (@typeInfo(@TypeOf(puzzle))) { + .Fn => |f| { + switch (@typeInfo(f.return_type.?)) { + .ErrorUnion => { + if (f.args.len == 1) + break :blk try puzzle(file_buffer); + break :blk try puzzle(allocator, file_buffer); + }, + else => { + if (f.args.len == 1) + break :blk puzzle(file_buffer); + break :blk puzzle(allocator, file_buffer); + }, + } + }, + else => unreachable, + } + }; + + if (result == expected) { + std.debug.print("day={d}, puzzle_{d}={d}\n", .{ day + 1, idx + 1, result }); + } else { + std.debug.print("day={d}, puzzle_{d}={d}, expected={d}\n", .{ day + 1, idx + 1, result, expected }); + } + } + } + + std.debug.print("\n", .{}); +} |