summaryrefslogtreecommitdiff
path: root/day_02.zig
diff options
context:
space:
mode:
authorChristian Segundo2022-12-05 17:00:08 +0100
committerChristian Segundo2022-12-05 22:16:11 +0100
commit061a5bae272f45db6dcde99746922735f9769d25 (patch)
treee4dd9b6d903930c5c1b76286b4a08beea6f59162 /day_02.zig
parent8817203517907ef4248bde7474e6fb566515d6a7 (diff)
downloadadvent-of-zig-2022-061a5bae272f45db6dcde99746922735f9769d25.tar.gz
add day 5
Diffstat (limited to 'day_02.zig')
-rw-r--r--day_02.zig110
1 files changed, 110 insertions, 0 deletions
diff --git a/day_02.zig b/day_02.zig
new file mode 100644
index 0000000..ce5e165
--- /dev/null
+++ b/day_02.zig
@@ -0,0 +1,110 @@
+const std = @import("std");
+const Result = @import("util/aoc.zig").Result;
+
+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 Player_B_KV = std.ComptimeStringMap(Hand, [_]KV_Hand{
+ .{ .@"0" = "X", .@"1" = .Rock },
+ .{ .@"0" = "Y", .@"1" = .Paper },
+ .{ .@"0" = "Z", .@"1" = .Scissors },
+});
+
+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),
+ }
+ },
+ }
+ }
+};
+
+pub fn puzzle_1(input: []const u8) Result {
+ var iter = std.mem.split(u8, input, "\n");
+
+ var score: u16 = 0;
+
+ while (iter.next()) |line| {
+ score += match_impl.puzzle_1(
+ Player_A_KV.get(&[_]u8{line[0]}).?,
+ Player_B_KV.get(&[_]u8{line[2]}).?,
+ );
+ }
+
+ return .{ .int = score };
+}
+
+pub fn puzzle_2(input: []const u8) Result {
+ 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 .{ .int = score };
+}