Created
August 23, 2023 16:17
-
-
Save mattfreire/45a8a5ef80ce4c7551c8c77d7d3a4c96 to your computer and use it in GitHub Desktop.
Advent of Code Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| const Move = enum { rock, paper, scissors }; | |
| const Result = enum { win, draw, loss }; | |
| const EncryptionError = error{UnrecognizedInput}; | |
| fn decryptMyMove(input: u8) EncryptionError!Move { | |
| const result: EncryptionError!Move = switch (input) { | |
| 'X' => .rock, | |
| 'Y' => .paper, | |
| 'Z' => .scissors, | |
| else => EncryptionError.UnrecognizedInput, | |
| }; | |
| return result; | |
| } | |
| fn decryptOpponentMove(input: u8) EncryptionError!Move { | |
| const result: EncryptionError!Move = switch (input) { | |
| 'A' => .rock, | |
| 'B' => .paper, | |
| 'C' => .scissors, | |
| else => EncryptionError.UnrecognizedInput, | |
| }; | |
| return result; | |
| } | |
| fn getHandResult(myMove: Move, opponentMove: Move) Result { | |
| const result: Result = switch (myMove) { | |
| .rock => switch (opponentMove) { | |
| .rock => Result.draw, | |
| .paper => Result.loss, | |
| .scissors => Result.win, | |
| }, | |
| .paper => switch (opponentMove) { | |
| .rock => Result.win, | |
| .paper => Result.draw, | |
| .scissors => Result.loss, | |
| }, | |
| .scissors => switch (opponentMove) { | |
| .rock => Result.loss, | |
| .paper => Result.win, | |
| .scissors => Result.draw, | |
| }, | |
| }; | |
| return result; | |
| } | |
| fn getMoveScore(move: Move) i32 { | |
| const result: i32 = switch (move) { | |
| .rock => 1, | |
| .paper => 2, | |
| .scissors => 3, | |
| }; | |
| return result; | |
| } | |
| fn getResultScore(result: Result) i32 { | |
| const score: i32 = switch (result) { | |
| .win => 6, | |
| .draw => 3, | |
| .loss => 0, | |
| }; | |
| return score; | |
| } | |
| fn getRoundScore(move: Move, result: Result) i32 { | |
| const move_score: i32 = getMoveScore(move); | |
| const result_score: i32 = getResultScore(result); | |
| return move_score + result_score; | |
| } | |
| pub fn main() !void { | |
| const fileName = "day2.txt"; | |
| const file = try std.fs.cwd().openFile(fileName, .{}); | |
| defer file.close(); | |
| var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | |
| defer arena.deinit(); | |
| const allocator = arena.allocator(); | |
| const read_buf = try file.readToEndAlloc(allocator, 1024 * 1024); | |
| defer allocator.free(read_buf); | |
| // try std.io.getStdOut().writeAll(read_buf); | |
| var totalScore: i32 = 0; | |
| var it = std.mem.tokenizeAny(u8, read_buf, "\n"); | |
| while (it.next()) |item| { | |
| const opponentMove: Move = try decryptOpponentMove(item[0]); | |
| const myMove: Move = try decryptMyMove(item[2]); | |
| std.debug.print("{}: {}\n", .{ myMove, opponentMove }); | |
| const result: Result = getHandResult(myMove, opponentMove); | |
| std.debug.print("Result for the round: {}\n", .{result}); | |
| const score: i32 = getRoundScore(myMove, result); | |
| std.debug.print("Score for the round: {}\n", .{score}); | |
| totalScore += score; | |
| } | |
| std.debug.print("Final score: {}\n", .{totalScore}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment