Skip to content

Commit

Permalink
2022 day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacerulerwill committed Jan 8, 2025
1 parent 99689c1 commit a6c2031
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions puzzles/Y2022/D2/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Any

choice_map = {"A": 1, "B": 2, "C": 3, "X": 1, "Y": 2, "Z": 3}


def puzzle(input: str) -> Any:
lines = input.strip().split("\n")
turns = [line.split() for line in lines]

def score_for_round(opponent: str, us: str) -> int:
score = 0
us_int = choice_map[us]
opponent_int = choice_map[opponent]
# always add our choice
score += us_int
# draw?
if opponent_int == us_int:
score += 3
# did we win? we always win if our choice is after theirs in order rock, paper, scissors
elif 1 + (us_int - 2) % 3 == opponent_int:
score += 6
return score

total_score = 0
for turn in turns:
total_score += score_for_round(turn[0], turn[1])
return total_score
24 changes: 24 additions & 0 deletions puzzles/Y2022/D2/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Any

choice_map = {"A": 1, "B": 2, "C": 3, "X": 1, "Y": 2, "Z": 3}


def puzzle(input: str) -> Any:
lines = input.strip().split("\n")

turns = [line.split() for line in lines]
# rock, paper, scissors
total_score = 0
for turn in turns:
opponent = choice_map[turn[0]]
us = choice_map[turn[1]]
if us == 1:
# we need to lose
total_score += 1 + (opponent - 2) % 3
elif us == 2:
# we need to draw - choose same as them
total_score += 3 + opponent
elif us == 3:
# we need to win the round - choice choice to right-
total_score += 6 + 1 + (opponent) % 3
return total_score

0 comments on commit a6c2031

Please sign in to comment.