AdventOfCode2022/Handlers/Day02.cs
2022-12-02 17:11:12 +01:00

98 lines
2.3 KiB
C#

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");
PartA();
// PartB();
}
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 GetGameResult(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;
}
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 = GetGameResult(opponent, you);
var points = (int)you + (int)result;
totalPoints += points;
Console.WriteLine($"Opponent: {opponent}, You: {you}, Result: {result}, Points: {points}");
}
Console.WriteLine($"Total points: {totalPoints}");
}
public void PartB()
{
throw new NotImplementedException();
}
}
}