day2 part 2

This commit is contained in:
Ruidy 2022-12-02 16:26:45 +01:00
parent 8cecf0653c
commit 96e7b21aac
2 changed files with 50 additions and 10 deletions

View file

@ -15,7 +15,8 @@ and `C` for Scissors. The second column--" Suddenly, the Elf is called away to h
The second column, you reason, must be what you should play in response: X for Rock, Y for Paper, and Z for Scissors.
Winning every time would be suspicious, so the responses must have been carefully chosen.
The winner of the whole tournament is the player with the highest score. Your **total score** is the sum of your scores for
The winner of the whole tournament is the player with the highest score. Your **total score** is the sum of your scores
for
each round. The score for a single round is the score for the **shape you selected** (1 for Rock, 2 for Paper, and 3 for
Scissors) plus the score for the **outcome of the round** (0 if you lost, 3 if the round was a draw, and 6 if you won).
@ -39,4 +40,22 @@ This strategy guide predicts and recommends the following:
- The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = **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?**

View file

@ -1,7 +1,4 @@
from typing import Generator
def total_score(data: Generator[list[str]]):
def total_score_1(data: list[list[str]]) -> int:
points = {
"A": 1,
"B": 2,
@ -26,9 +23,33 @@ def total_score(data: Generator[list[str]]):
return score
def read_data() -> Generator[list[str]]:
def total_score_2(data: list[list[str]]) -> int:
points = {
"A": 0,
"B": 1,
"C": 2,
"X": 1,
"Y": 0,
"Z": 2,
}
score = 0
for row in data:
a, exp = row
if points[exp] == 0:
# it's a draw
score += 3 + points[a] + 1
elif points[exp] == 2:
# won
score += 6 + (points[a] + 1) % 3 + 1
else:
# lose
score += (points[a] - 1) % 3 + 1
return score
def read_data() -> list[list[str]]:
with open("./input.txt", "r") as f:
return (row.strip().split(" ") for row in f.readlines())
return [row.strip().split(" ") for row in f.readlines()]
if __name__ == "__main__":
@ -37,5 +58,5 @@ if __name__ == "__main__":
("B", "X"),
("C", "Z"),
]
data = read_data()
print(total_score(data))
dataset = read_data()
print(total_score_2(dataset))