Day 2 - Part B
This commit is contained in:
parent
f5ee963bba
commit
c428835d24
@ -21,8 +21,6 @@ namespace AdventOfCode2022.Handlers
|
|||||||
public Day02()
|
public Day02()
|
||||||
{
|
{
|
||||||
rounds = System.IO.File.ReadAllText(@"./days/02/input").Trim().Split("\n");
|
rounds = System.IO.File.ReadAllText(@"./days/02/input").Trim().Split("\n");
|
||||||
PartA();
|
|
||||||
// PartB();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Choice ParseChoice(string input)
|
private Choice ParseChoice(string input)
|
||||||
@ -46,7 +44,7 @@ namespace AdventOfCode2022.Handlers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private GameResult GetGameResult(Choice opponent, Choice you)
|
private GameResult PartAGameResult(Choice opponent, Choice you)
|
||||||
{
|
{
|
||||||
if (you == opponent)
|
if (you == opponent)
|
||||||
{
|
{
|
||||||
@ -71,6 +69,24 @@ namespace AdventOfCode2022.Handlers
|
|||||||
return GameResult.Loss;
|
return GameResult.Loss;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void PartA()
|
public void PartA()
|
||||||
{
|
{
|
||||||
var totalPoints = 0;
|
var totalPoints = 0;
|
||||||
@ -80,19 +96,67 @@ namespace AdventOfCode2022.Handlers
|
|||||||
var parts = line.Split(" ");
|
var parts = line.Split(" ");
|
||||||
var opponent = ParseChoice(parts[0]);
|
var opponent = ParseChoice(parts[0]);
|
||||||
var you = ParseChoice(parts[1]);
|
var you = ParseChoice(parts[1]);
|
||||||
var result = GetGameResult(opponent, you);
|
var result = PartAGameResult(opponent, you);
|
||||||
|
|
||||||
var points = (int)you + (int)result;
|
var points = (int)you + (int)result;
|
||||||
totalPoints += points;
|
totalPoints += points;
|
||||||
Console.WriteLine($"Opponent: {opponent}, You: {you}, Result: {result}, Points: {points}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Total points: {totalPoints}");
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PartB()
|
public void PartB()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,6 +20,7 @@ namespace AdventOfCode2022
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void dayOne() {
|
static void dayOne() {
|
||||||
|
Console.WriteLine("Day 01");
|
||||||
var day = new Day01();
|
var day = new Day01();
|
||||||
day.PartA();
|
day.PartA();
|
||||||
day.PartB();
|
day.PartB();
|
||||||
@ -28,8 +29,10 @@ namespace AdventOfCode2022
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void dayTwo() {
|
static void dayTwo() {
|
||||||
|
Console.WriteLine("Day 02");
|
||||||
var day = new Day02();
|
var day = new Day02();
|
||||||
day.PartA();
|
day.PartA();
|
||||||
|
day.PartB();
|
||||||
|
|
||||||
PrintSeparator();
|
PrintSeparator();
|
||||||
}
|
}
|
||||||
|
@ -29,3 +29,18 @@ This strategy guide predicts and recommends the following:
|
|||||||
In this example, if you were to follow the strategy guide, you would get a total score of `_15_` (8 + 1 + 6).
|
In this example, if you were to follow the strategy guide, you would get a total score of `_15_` (8 + 1 + 6).
|
||||||
|
|
||||||
_What would your total score be if everything goes exactly according to your strategy guide?_
|
_What would your total score be if everything goes exactly according to your strategy guide?_
|
||||||
|
|
||||||
|
\--- Part Two ---
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: `X` means you need to lose, `Y` means you need to end the round in a draw, and `Z` means you need to win. Good luck!"
|
||||||
|
|
||||||
|
The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this:
|
||||||
|
|
||||||
|
* In the first round, your opponent will choose Rock (`A`), and you need the round to end in a draw (`Y`), so you also choose Rock. This gives you a score of 1 + 3 = _4_.
|
||||||
|
* In the second round, your opponent will choose Paper (`B`), and you choose Rock so you lose (`X`) with a score of 1 + 0 = _1_.
|
||||||
|
* In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = _7_.
|
||||||
|
|
||||||
|
Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of `_12_`.
|
||||||
|
|
||||||
|
Following the Elf's instructions for the second column, _what would your total score be if everything goes exactly according to your strategy guide?_
|
||||||
|
Loading…
Reference in New Issue
Block a user