2022 - Day 2 (Rock Paper Scissors)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3

#Advent of Code
#2022 Day 2, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    class Shape:
        def __init__(self, letter):
            match letter:
                case "A" | "X":
                    self.shape = "rock"
                    self.score = 1
                case "B" | "Y":
                    self.shape = "paper"
                    self.score = 2
                case "C" | "Z":
                    self.shape = "scissors"
                    self.score = 3

        def beats(self, opponent):
            match self.shape:
                case "rock":     return opponent.shape == "scissors"
                case "paper":    return opponent.shape == "rock"
                case "scissors": return opponent.shape == "paper"

        def __eq__(self, other):
            return self.shape == other.shape

    def round_outcome(opponent, player):
        opponent = Shape(opponent)
        player   = Shape(player)

        if opponent.beats(player):
            # The opponent's move wins
            return 0
        elif opponent == player:
            # It's a tie
            return 3
        else:
            # The player's move wins
            return 6

    return sum(Shape(player).score + round_outcome(opponent, player) for opponent, player in [turn.split() for turn in puzzle_input])

def main():
    puzzle_input = util.read.as_lines()

    score = solve(puzzle_input)

    print("The total score is " + str(score) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["A Y",
                                       "B X",
                                       "C Z"]), 15)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3

#Advent of Code
#2022 Day 2, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    class Shape:
        def __init__(self, letter):
            match letter:
                case "A" | "rock":
                    self.shape    = "rock"
                    self.score    = 1
                    self.wins_to  = "scissors"
                    self.loses_to = "paper"
                case "B" | "paper":
                    self.shape    = "paper"
                    self.score    = 2
                    self.wins_to  = "rock"
                    self.loses_to = "scissors"
                case "C" | "scissors":
                    self.shape    = "scissors"
                    self.score    = 3
                    self.wins_to  = "paper"
                    self.loses_to = "rock"

        def beats(self, opponent):
            return self.wins_to == opponent.shape

        def __eq__(self, other):
            return self.shape == other.shape

    def round_outcome(opponent, player):
        if opponent.beats(player):
            # The opponent's move wins
            return 0
        elif opponent == player:
            # It's a tie
            return 3
        else:
            # The player's move wins
            return 6

    total = 0
    for turn in puzzle_input:
        opponent, desired_outcome = turn.split()
        opponent = Shape(opponent)
        match desired_outcome:
            case "X":
                # Need to lose
                # Play the move that the opponent beats
                player = Shape(opponent.wins_to)
            case "Y":
                # Need to tie
                # Play the same move as the opponent
                player = opponent
            case "Z":
                # Need to win
                # Play the move that beats the opponent, i.e. the move that the opponent is beaten by
                player = Shape(opponent.loses_to)

        total += player.score + round_outcome(opponent, player)

    return total

def main():
    puzzle_input = util.read.as_lines()

    score = solve(puzzle_input)

    print("The total score is " + str(score) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["A Y",
                                       "B X",
                                       "C Z"]), 12)

if __name__ == "__main__":
    run(main)
Return to solution index