134 lines
4.0 KiB
C#
134 lines
4.0 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Day02CSharp
|
|
{
|
|
internal class Program
|
|
{
|
|
enum Hand
|
|
{
|
|
Rock = 1, Paper = 2, Scissors = 3
|
|
}
|
|
|
|
static Hand GetHand(int i)
|
|
{
|
|
switch (i)
|
|
{
|
|
case 1:
|
|
return Hand.Rock;
|
|
case 2:
|
|
return Hand.Paper;
|
|
case 3:
|
|
return Hand.Scissors;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
static int GetScore(Hand left, Hand right)
|
|
{
|
|
if (left == right) return 3;
|
|
|
|
if ( left == Hand.Rock && right == Hand.Paper
|
|
|| left == Hand.Paper && right == Hand.Scissors
|
|
|| left == Hand.Scissors && right == Hand.Rock
|
|
) { return 6; }
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int GetBeater(Hand h)
|
|
{
|
|
switch (h)
|
|
{
|
|
case Hand.Rock: return (int)Hand.Paper;
|
|
case Hand.Scissors: return (int)Hand.Rock;
|
|
case Hand.Paper: return (int)Hand.Scissors;
|
|
default: throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
static int GetLoser(Hand h)
|
|
{
|
|
switch (h)
|
|
{
|
|
case Hand.Rock: return (int)Hand.Scissors;
|
|
case Hand.Scissors: return (int)Hand.Paper;
|
|
case Hand.Paper: return (int)Hand.Rock;
|
|
default: throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
Part01();
|
|
Part02();
|
|
}
|
|
|
|
private static void Part01()
|
|
{
|
|
var fileName = @"data\input.txt";
|
|
int currentScore = 0;
|
|
|
|
using (StreamReader reader = File.OpenText(fileName))
|
|
{
|
|
while (!reader.EndOfStream)
|
|
{
|
|
string line = reader.ReadLine();
|
|
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (split.Length == 2)
|
|
{
|
|
int enemy = split[0][0] - 'A' + 1;
|
|
int yours = split[1][0] - 'X' + 1;
|
|
|
|
// Add in selected shape
|
|
currentScore += yours;
|
|
currentScore += GetScore(GetHand(enemy), GetHand(yours));
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine(currentScore);
|
|
}
|
|
|
|
private static void Part02()
|
|
{
|
|
var fileName = @"data\input.txt";
|
|
int currentScore = 0;
|
|
|
|
using (StreamReader reader = File.OpenText(fileName))
|
|
{
|
|
while (!reader.EndOfStream)
|
|
{
|
|
string line = reader.ReadLine();
|
|
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (split.Length == 2)
|
|
{
|
|
int enemy = split[0][0] - 'A' + 1;
|
|
int yours = split[1][0];
|
|
|
|
switch (yours)
|
|
{
|
|
case 'X': // lose
|
|
currentScore += GetLoser(GetHand(enemy));
|
|
break;
|
|
case 'Y': // draw
|
|
currentScore += enemy + 3;
|
|
break;
|
|
case 'Z': // win
|
|
currentScore += GetBeater(GetHand(enemy)) + 6;
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine(currentScore);
|
|
}
|
|
}
|
|
}
|