2022-12-02 17:11:12 +01:00
|
|
|
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}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:14:31 +01:00
|
|
|
private GameResult PartAGameResult(Choice opponent, Choice you)
|
2022-12-02 17:11:12 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:14:31 +01:00
|
|
|
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}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:11:12 +01:00
|
|
|
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]);
|
2022-12-03 00:14:31 +01:00
|
|
|
var result = PartAGameResult(opponent, you);
|
2022-12-02 17:11:12 +01:00
|
|
|
|
|
|
|
var points = (int)you + (int)result;
|
|
|
|
totalPoints += points;
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:14:31 +01:00
|
|
|
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;
|
2022-12-02 17:11:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void PartB()
|
|
|
|
{
|
2022-12-03 00:14:31 +01:00
|
|
|
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}");
|
2022-12-02 17:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|