Advent Of Code 2022 C#: Day 2 Rock Paper, Scissors

Goal: Cheat at Rock Paper Scissors

Josh P (Pixel Grim)
4 min readDec 2, 2022

Start the challenge yourself here -> https://adventofcode.com/2022

Spoilers

Day 2 of Advent of Code, and my head hurts! I started this challenge last night around 12 am… Probably not a great choice, but I was eager to see what lay ahead. There are two parts, as usual, so in the first part of the challenge, you are in a rock paper scissor tournament. The winner is the one with the most points. Points are determined by the hand you played and the outcome of the round. Rock = 1, Paper = 2, and Scissor =3. Wining = 6, tie = 3, and lose = 0. But the twist is you are given a cheat sheet of what your opponent is going to play and what you must play in order to win! But it is coded as A & X = Rock, B & Y = Paper, and C & Z = Scissor. An example of input is below.

C Z
C X
B Z
A X
A Z
C Z
A Y
C Z
C Z
A X

For part two, the user inputs change instead of XYX being Rock, Paper, or Scissor. XYX means to win, lose or tie and find what hand to win to get that result.

My Code:

Link to entire code: GitHub

A created a bool to help determine what outcome I wanted either for Part 1 or Part 2. Next, I read the file and split the text into a tuple, one for elf input and one for the player inputs. I had a method called start game that will begin the game(calculate the outcomes) and end when we reached the max amount of rounds.

int _totalScore = 0;
string _changeWinStateString;

Console.WriteLine("PART 1 SCORE: FALSE, PART 2 SCORE: TRUE - Input: ");
_changeWinStateString = Console.ReadLine();
bool _changeWinState = _changeWinStateString == "TRUE";

string[] lines = File.ReadAllLines(@"C:\Users\17168\Desktop\RPSCheat.txt");

(string, string)[] letters = new (string, string)[lines.Length];

SplitLetters();
startGame();
Console.WriteLine("TOTAL SCORE: " + _totalScore); // GAME IS OVER FINAL OUTCOME OF BOTH PARTS

Once the game started, we began figuring out the outcome! The code is a bit messy, but I’m learning as time goes on! I’m just grateful it works. We start by converting the letters into ROCK, PAPER, and SCISSOR and then pass that onto a method that will calculate the score and a method that will determine a win, lose or tie. For part 2, I have a bool set in place that will use another method to help decide the user what hand to play if we want to win, lose or tie on purpose. Lastly, we calculate the score and print it to the console!

void ConvertInputToRSP((string, string)[] inputs, int round, bool codeWin)
{
RPS playerInput = RPS.Rock;
RPS elfInput = RPS.Rock;
WinLoseTie winlosetie = WinLoseTie.Lose;
if (!codeWin)
{
if (inputs[round].Item1 == "A") elfInput = RPS.Rock;
if (inputs[round].Item1 == "B") elfInput = RPS.Paper;
if (inputs[round].Item1 == "C") elfInput = RPS.Scissor;

if (inputs[round].Item2 == "X") playerInput = RPS.Rock;
if (inputs[round].Item2 == "Y") playerInput = RPS.Paper;
if (inputs[round].Item2 == "Z") playerInput = RPS.Scissor;
CalculateScore(playerInput.ToString());
DetermineWinRSP(playerInput.ToString(), elfInput.ToString());
}
else if (codeWin)
{

if (inputs[round].Item1 == "A") elfInput = RPS.Rock;
if (inputs[round].Item1 == "B") elfInput = RPS.Paper;
if (inputs[round].Item1 == "C") elfInput = RPS.Scissor;

if (inputs[round].Item2 == "X") winlosetie = WinLoseTie.Lose;
if (inputs[round].Item2 == "Y") winlosetie = WinLoseTie.Tie;
if (inputs[round].Item2 == "Z") winlosetie = WinLoseTie.Win;
CalculateScore(winlosetie.ToString());
DetermineWinLoseStart(elfInput.ToString(),winlosetie.ToString());
}
}


void DetermineWinLoseStart(string elf, string wtl)
{
RPS playerInput = RPS.Rock;
if((elf == "Paper" && wtl == "Lose") || (elf == "Rock" && wtl == "Tie") || (elf == "Scissor" && wtl == "Win")) { playerInput = RPS.Rock; }
if ((elf == "Rock" && wtl == "Win") || (elf == "Paper" && wtl == "Tie") || (elf == "Scissor"&& wtl == "Lose")) { playerInput = RPS.Paper; }
if ((elf == "Paper" && wtl == "Win") || (elf == "Scissor" && wtl == "Tie") || (elf == "Rock" && wtl == "Lose")) { playerInput = RPS.Scissor; }
CalculateScore(playerInput.ToString());

}


void DetermineWinRSP(string user, string elf)
{
WinLoseTie result = WinLoseTie.Lose;

if (user == "Rock" && elf == "Rock") { result = WinLoseTie.Tie; Console.WriteLine("TIE"); }
if (user == "Paper" && elf == "Paper") { result = WinLoseTie.Tie; Console.WriteLine("TIE"); }
if (user == "Scissor" && elf == "Scissor") { result = WinLoseTie.Tie; Console.WriteLine("TIE"); }
if (user == "Rock" && elf == "Paper") { result = WinLoseTie.Lose; Console.WriteLine("LOSE BY PAPER"); }
if (user == "Paper" && elf == "Scissor") { result = WinLoseTie.Lose; Console.WriteLine("LOSE BY SCISSOR"); }
if (user == "Scissor" && elf == "Rock") { result = WinLoseTie.Lose; Console.WriteLine("LOSE BY ROCK"); }
if (user == "Rock" && elf == "Scissor") { result = WinLoseTie.Win; Console.WriteLine("WIN BY ROCK"); }
if (user == "Paper" && elf == "Rock") { result = WinLoseTie.Win; Console.WriteLine("WIN BY paper"); }
if (user == "Scissor" && elf == "Paper") { result = WinLoseTie.Win; Console.WriteLine("WIN BY scissor"); }
CalculateScore(result.ToString());
}


void CalculateScore(string score)
{

if (score == "Rock") _totalScore += 1;
if (score == "Paper") _totalScore += 2;
if (score == "Scissor") _totalScore += 3;
if (score == "Win") _totalScore += 6;
if (score == "Lose") _totalScore += 0;
if (score == "Tie") _totalScore += 3;
}

--

--