(update) 2022 day 2
This commit is contained in:
48
2022/02/main.py
Normal file
48
2022/02/main.py
Normal file
@ -0,0 +1,48 @@
|
||||
SCORES = [
|
||||
{"id": "A", "name": "rock", "wins_over": "C", "points": 1},
|
||||
{"id": "B", "name": "paper", "wins_over": "A", "points": 2},
|
||||
{"id": "C", "name": "scissors", "wins_over": "B", "points": 3},
|
||||
{"id": "X", "name": "rock", "wins_over": "C", "points": 1, "strategy": "lose"},
|
||||
{"id": "Y", "name": "paper", "wins_over": "A", "points": 2, "strategy": "draw"},
|
||||
{"id": "Z", "name": "scissors", "wins_over": "B", "points": 3, "strategy": "win"},
|
||||
]
|
||||
|
||||
with open("input", "+r") as file:
|
||||
total_score = 0
|
||||
for line in file.readlines():
|
||||
them, us = map(lambda x: x.strip(), line.split())
|
||||
our_game = list(filter(lambda x: x["id"] == us, SCORES))[0]
|
||||
their_game = list(filter(lambda x: x["id"] == them, SCORES))[0]
|
||||
|
||||
if our_game["wins_over"] == them:
|
||||
total_score += our_game["points"] + 6
|
||||
continue
|
||||
|
||||
if our_game["name"] == their_game["name"]:
|
||||
total_score += our_game["points"] + 3
|
||||
continue
|
||||
|
||||
total_score += our_game["points"]
|
||||
|
||||
print("part 1:", total_score)
|
||||
|
||||
with open("input", "+r") as file:
|
||||
total_score = 0
|
||||
for line in file.readlines():
|
||||
them, us = map(lambda x: x.strip(), line.split())
|
||||
our_game = list(filter(lambda x: x["id"] == us, SCORES))[0]
|
||||
their_game = list(filter(lambda x: x["id"] == them, SCORES))[0]
|
||||
|
||||
match our_game["strategy"]:
|
||||
case "lose":
|
||||
strategy = list(
|
||||
filter(lambda x: x["id"] == their_game["wins_over"], SCORES)
|
||||
)
|
||||
total_score += strategy[0]["points"]
|
||||
case "win":
|
||||
strategy = list(filter(lambda x: x["wins_over"] == them, SCORES))
|
||||
total_score += 6 + strategy[0]["points"]
|
||||
case _:
|
||||
total_score += 3 + their_game["points"]
|
||||
|
||||
print("part 2:", total_score)
|
||||
Reference in New Issue
Block a user