2022-12-02 - Rock Paper Scissors
(original .ipynb)
Day 2 puzzle input is a sequence of either A
, B
or C
followed by either X
, Y
or Z
(mine is here). Part 1 involves interpreting these as a set of instructions as to what you and an opponent will play in rock, paper scissors and calculating a score. Part 2 involves interpreting these slightly differently (what your opponent will play, and whether you should win, lose or draw) then calculating the score as before.
puzzle_input_str = open("./puzzle_input/day2.txt").read() test_input_str = """A Y B X C Z""" rock, paper, scissors = ("A", "B", "C") shape_score = {rock: 1, paper: 2, scissors: 3} defeats = {rock: paper, paper: scissors, scissors: rock} defeated_by = {rock: scissors, paper: rock, scissors: paper} def guessed_parser(line): # guess that X/Y/Z maps to rock/paper/scissors first, second = line.split(" ") return first, chr(ord(second) + ord(rock) - ord("X")) def part_one(input_str, parse_line=guessed_parser): my_score = 0 for line in input_str.split("\n"): theirs, mine = parse_line(line) my_score += shape_score[mine] if theirs == mine: my_score += 3 elif defeated_by[mine] == theirs: my_score += 6 return my_score assert 15 == part_one(test_input_str) print("part one:", part_one(puzzle_input_str))
part one: 11150
def outcome_parser(line): theirs, outcome = line.split(" ") if outcome == "X": # lose return (theirs, defeated_by[theirs]) elif outcome == "Y": # draw return (theirs, theirs) else: # win return (theirs, defeats[theirs]) def part_two(input_str): return part_one(input_str, outcome_parser) assert 12 == part_two(test_input_str) print("part two:", part_two(puzzle_input_str))
part two: 8295