AdventOfCode/Advent Of Code Library/2022/Day 02/Day02.cs
2022-12-03 17:00:24 +01:00

82 lines
3.0 KiB
C#

namespace Day_02.Day_02
{
public 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
{ OpponentRock + " " + PlayPaper, Paper + Win }, // win
{ OpponentRock + " " + PlayRock, Rock + Draw }, // draw
{ OpponentRock + " " + PlayScissors, Scissors + Lost }, // lose
// Paper
{ OpponentPaper + " " + PlayScissors, Scissors + Win }, // win
{ OpponentPaper + " " + PlayPaper, Paper + Draw }, // draw
{ OpponentPaper + " " + PlayRock, Rock + Lost }, // lose
// Scissors
{ OpponentScissors + " " + PlayRock, Rock + Win }, // win
{ OpponentScissors + " " + PlayScissors, Scissors + Draw }, // draw
{ OpponentScissors + " " + PlayPaper, Paper + Lost } // lose
};
}
}
private static void BuildWinDrawLoseTable()
{
if (PossibleScores == null)
{
PossibleScores = new Dictionary<string, int>
{
// Rock combos
{ OpponentRock + " " + PlayWin, Paper + Win }, // win
{ OpponentRock + " " + PlayDraw, Rock + Draw }, // draw
{ OpponentRock + " " + PlayLose, Scissors + Lost }, // lose
// Paper
{ OpponentPaper + " " + PlayWin, Scissors + Win }, // win
{ OpponentPaper + " " + PlayDraw, Paper + Draw }, // draw
{ OpponentPaper + " " + PlayLose, Rock + Lost }, // lose
// Scissors
{ OpponentScissors + " " + PlayWin, Rock + Win }, // win
{ OpponentScissors + " " + PlayDraw, Scissors + Draw }, // draw
{ OpponentScissors + " " + PlayLose, Paper + Lost } // lose
};
}
}
private static int GetScore(string input) => PossibleScores[input];
public static int GetPart1()
{
BuildScoreDic();
return File.ReadAllLines(inputPath)
.Select(GetScore)
.Sum();
}
public static int GetPart2()
{
BuildWinDrawLoseTable();
return File.ReadAllLines(inputPath)
.Select(GetScore)
.Sum();
}
}
}