AdventOfCode/Day 02/Day02.cs
2022-12-02 11:49:06 +01:00

80 lines
3.3 KiB
C#

namespace Day_02.Day_02
{
internal class Day02
{
private static string inputPath = "Day 02/day-02-input.txt";
private static int Win = 6, Draw = 3, Lost = 0, Rock = 1, Paper = 2, Scissors = 3;
private static string OpponentRock = "A", OpponentPaper = "B", OpponentScissors = "C",
PlayRock = "X", PlayPaper = "Y", PlayScissors = "Z",
PlayWin = "Z", PlayDraw = "Y", PlayLose = "X";
private static Dictionary<string, int> PossibleScores;
private static void BuildScoreDic()
{
if (PossibleScores == null)
{
PossibleScores = new Dictionary<string, int>();
// Rock combos
PossibleScores.Add(OpponentRock + " " + PlayPaper, Paper + Win); // win
PossibleScores.Add(OpponentRock + " " + PlayRock, Rock + Draw); // draw
PossibleScores.Add(OpponentRock + " " + PlayScissors, Scissors + Lost); // lose
// Paper
PossibleScores.Add(OpponentPaper + " " + PlayScissors, Scissors + Win); // win
PossibleScores.Add(OpponentPaper + " " + PlayPaper, Paper + Draw); // draw
PossibleScores.Add(OpponentPaper + " " + PlayRock, Rock + Lost); // lose
// Scissors
PossibleScores.Add(OpponentScissors + " " + PlayRock, Rock + Win); // win
PossibleScores.Add(OpponentScissors + " " + PlayScissors, Scissors + Draw); // draw
PossibleScores.Add(OpponentScissors + " " + PlayPaper, Paper + Lost); // lose
}
}
private static void BuildWinDrawLoseTable()
{
if (PossibleScores == null)
{
PossibleScores = new Dictionary<string, int>();
// Rock combos
PossibleScores.Add(OpponentRock + " " + PlayWin, Paper + Win); // win
PossibleScores.Add(OpponentRock + " " + PlayDraw, Rock + Draw); // draw
PossibleScores.Add(OpponentRock + " " + PlayLose, Scissors + Lost); // lose
// Paper
PossibleScores.Add(OpponentPaper + " " + PlayWin, Scissors + Win); // win
PossibleScores.Add(OpponentPaper + " " + PlayDraw, Paper + Draw); // draw
PossibleScores.Add(OpponentPaper + " " + PlayLose, Rock + Lost); // lose
// Scissors
PossibleScores.Add(OpponentScissors + " " + PlayWin, Rock + Win); // win
PossibleScores.Add(OpponentScissors + " " + PlayDraw, Scissors + Draw); // draw
PossibleScores.Add(OpponentScissors + " " + PlayLose, Paper + Lost); // lose
}
}
private static int GetScore(string input) => PossibleScores[input];
internal static int GetPart1()
{
BuildScoreDic();
return File.ReadAllLines(inputPath)
.Select(GetScore)
.Sum();
}
internal static int GetPart2()
{
BuildWinDrawLoseTable();
return File.ReadAllLines(inputPath)
.Select(GetScore)
.Sum();
}
}
}