namespace AdventOfCode2022.Handlers { public class Day02 { public enum GameResult { Loss = 0, Draw = 3, Win = 6 } public enum Choice { Rock = 1, Paper = 2, Scissors = 3 } string[] rounds; public Day02() { rounds = System.IO.File.ReadAllText(@"./days/02/input").Trim().Split("\n"); } private Choice ParseChoice(string input) { switch (input) { case "A": case "X": return Choice.Rock; case "B": case "Y": return Choice.Paper; case "C": case "Z": return Choice.Scissors; default: throw new Exception($"Invalid choice: {input}"); } } private GameResult PartAGameResult(Choice opponent, Choice you) { if (you == opponent) { return GameResult.Draw; } if (you == Choice.Rock && opponent == Choice.Scissors) { return GameResult.Win; } if (you == Choice.Paper && opponent == Choice.Rock) { return GameResult.Win; } if (you == Choice.Scissors && opponent == Choice.Paper) { return GameResult.Win; } return GameResult.Loss; } private GameResult PartBGameResult(string input) { switch (input) { case "X": return GameResult.Loss; case "Y": return GameResult.Draw; case "Z": return GameResult.Win; default: throw new Exception($"Invalid choice: {input}"); } } public void PartA() { var totalPoints = 0; foreach (var line in rounds) { var parts = line.Split(" "); var opponent = ParseChoice(parts[0]); var you = ParseChoice(parts[1]); var result = PartAGameResult(opponent, you); var points = (int)you + (int)result; totalPoints += points; } Console.WriteLine($"Part A - Total points: {totalPoints}"); } public Choice PartBChoiceFromResult(GameResult result, Choice opponent) { if (result.Equals(GameResult.Win)) { switch (opponent) { case Choice.Rock: return Choice.Paper; case Choice.Paper: return Choice.Scissors; case Choice.Scissors: return Choice.Rock; } } if (result.Equals(GameResult.Loss)) { switch (opponent) { case Choice.Rock: return Choice.Scissors; case Choice.Paper: return Choice.Rock; case Choice.Scissors: return Choice.Paper; } } // Draw return opponent; } public void PartB() { var totalPoints = 0; foreach (var line in rounds) { var parts = line.Split(" "); var opponent = ParseChoice(parts[0]); var result = PartBGameResult(parts[1]); Choice? you = PartBChoiceFromResult(result, opponent); totalPoints += (int)you + (int)result; } Console.WriteLine($"Part B - Total points: {totalPoints}"); } } }