Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve 2022 day 02 #13

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4f502d4
Create solution file for part 1
katzuv Dec 9, 2022
717483c
Add function that parses the input
katzuv Dec 9, 2022
f03f539
Create Python module for shapes constants
katzuv Dec 9, 2022
1441ba3
Add constants for the shapes
katzuv Dec 9, 2022
7425b50
Add dictionary that maps what opponent's shape is defeated by our shape
katzuv Dec 9, 2022
f71763d
Add dictionary that maps what shapes are identical
katzuv Dec 9, 2022
0450409
Add dictionary that maps shapes to their respective scores
katzuv Dec 9, 2022
74025e4
Add function that calculates a move's score
katzuv Dec 9, 2022
0a70508
Add function that calculates total score
katzuv Dec 9, 2022
334cbab
Create solution file for part 2
katzuv Dec 9, 2022
5c22100
Add constants for letters and outcomes
katzuv Dec 9, 2022
661570b
Add dictionary that maps what opponent's shape defeats our shape
katzuv Dec 9, 2022
30d3678
Add function that returns what shape should be played
katzuv Dec 9, 2022
6f652cb
Add function that generate moves based on opponent moves and outcomes
katzuv Dec 9, 2022
76578a1
Add function that calculates total score
katzuv Dec 10, 2022
7ac5567
Return tuples instead of lists
katzuv Dec 10, 2022
0bbe1de
Replace "couple" vs "pair"
katzuv Dec 10, 2022
de7da71
Remove wrong word
katzuv Dec 10, 2022
ed1911f
Remove "our" from shapes names
katzuv Dec 10, 2022
9de1b77
Move identical shapes list up and update names
katzuv Dec 10, 2022
2c84a14
Normalize input and use generic-shapes instead of player-specific
katzuv Dec 10, 2022
319fa59
Refactor winning moves mapping
katzuv Dec 10, 2022
e0a8032
Refactor losing moves mapping
katzuv Dec 10, 2022
59c7692
Add constant for draw score
katzuv Dec 10, 2022
3da780b
Add constant for win score
katzuv Dec 10, 2022
7ac710d
Use new constants in move score function
katzuv Dec 10, 2022
1fe46e1
Add draw moves mapping
katzuv Dec 10, 2022
7a78987
Add mapping of outcome to shapes mapping
katzuv Dec 10, 2022
0577bd0
Use new mappings mapping
katzuv Dec 10, 2022
1a53dc3
Reformat function definition
katzuv Dec 10, 2022
1b5a254
Remove now unused outcome constants
katzuv Dec 10, 2022
bc6f747
Fix finding the desired shape
katzuv Dec 10, 2022
de1feba
Replace tuple items accessing with existing variables
katzuv Dec 10, 2022
8439278
Rename `shapes.py` to `consts.py`
katzuv Dec 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions puzzles/solutions/2022/d02/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ROCK = "X"
PAPER = "Y"
SCISSORS = "Z"
OPPONENT_ROCK = "A"
OPPONENT_PAPER = "B"
OPPONENT_SCISSORS = "C"

IDENTICAL_SHAPES = {
OPPONENT_ROCK: ROCK,
OPPONENT_PAPER: PAPER,
OPPONENT_SCISSORS: SCISSORS,
}

WINNING_MOVES = {
ROCK: SCISSORS,
PAPER: ROCK,
SCISSORS: PAPER,
}
DRAW_MOVES = {shape: shape for shape in (ROCK, PAPER, SCISSORS)}
LOSING_MOVES = {
ROCK: PAPER,
PAPER: SCISSORS,
SCISSORS: ROCK,
}
OUTCOME_TO_MAPPING = {"X": LOSING_MOVES, "Y": DRAW_MOVES, "Z": WINNING_MOVES}

DRAW_SCORE = 3
WIN_SCORE = 6

SHAPE_TO_SCORE = {ROCK: 1, PAPER: 2, SCISSORS: 3}
45 changes: 45 additions & 0 deletions puzzles/solutions/2022/d02/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys

import consts


def get_moves(input_text: str) -> tuple[tuple[str, str], ...]:
"""
:param input_text: puzzle input
:return: list of (opponent move, our move) pairs
"""
input_text = input_text.translate(
str.maketrans(consts.IDENTICAL_SHAPES)
) # Replace opponent shapes with generic shapes.
moves = input_text.splitlines()
return tuple(tuple(move.split()) for move in moves)


def calculate_move_score(move: tuple[str, str]) -> int:
"""
:param move: pair of (opponent move, our move)
:return: total score of the move
"""
score = 0
opponent_move, our_move = move
if consts.WINNING_MOVES[our_move] == opponent_move:
score += consts.WIN_SCORE
elif opponent_move == our_move:
score += consts.DRAW_SCORE

score += consts.SHAPE_TO_SCORE[our_move]
return score


def get_answer(input_text: str):
"""Return the total score if everything goes exactly according to our strategy guide."""
moves = get_moves(input_text)
total_score = sum(calculate_move_score(move) for move in moves)
return total_score


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
44 changes: 44 additions & 0 deletions puzzles/solutions/2022/d02/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys

import consts
import p1


def choose_shape(opponent_move: str, outcome: str) -> str:
"""
:param opponent_move: move the opponent played
:param outcome: desired outcome of the move
:return: shape to play for the desired outcome to happen
"""
mapping = consts.OUTCOME_TO_MAPPING[outcome]
for our_move in mapping:
if mapping[our_move] == opponent_move:
return our_move


def generate_moves(
moves_outcomes: tuple[tuple[str, str]]
) -> tuple[tuple[str, str], ...]:
"""
:param moves_outcomes: list of (opponent move, desired outcome) pairs
:return: list of (opponent move, desired move for desired outcome to happen) pairs
"""
return tuple(
(opponent_move, choose_shape(opponent_move, outcome))
for opponent_move, outcome in moves_outcomes
)


def get_answer(input_text: str):
"""Return the total score if everything goes exactly according to our actual strategy guide."""
moves_outcomes = p1.get_moves(input_text)
moves = generate_moves(moves_outcomes)
total_score = sum(p1.calculate_move_score(move) for move in moves)
return total_score


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.