-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2.py
129 lines (98 loc) · 4.07 KB
/
solution_2.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
HANDS_CIPHER = dict(A="r", B="p", C="s", X="r", Y="p", Z="s")
OUTCOME_CIPHER = dict(X="loss", Y="draw", Z="win")
HAND_SCORES = dict(r=1, p=2, s=3)
ROUND_SCORES = dict(loss=0, draw=3, win=6)
INPUT_PATH = "inputs/input_2.txt"
from utils import utils
def decrypt_hand(encrypted_hand: str) -> str:
return HANDS_CIPHER[encrypted_hand]
def decrypt_outcome(encrypted_outcome: str) -> str:
return OUTCOME_CIPHER[encrypted_outcome]
def get_hand_score(hand: str) -> int:
return HAND_SCORES[hand]
def get_outcome_score(outcome: str) -> int:
return ROUND_SCORES[outcome]
def get_round_outcome(hand: str, opposing_hand: str) -> str:
if hand == opposing_hand:
return "draw"
if hand == "r" and opposing_hand == "s":
return "win"
if hand == "r" and opposing_hand == "p":
return "loss"
if hand == "p" and opposing_hand == "r":
return "win"
if hand == "p" and opposing_hand == "s":
return "loss"
if hand == "s" and opposing_hand == "p":
return "win"
if hand == "s" and opposing_hand == "r":
return "loss"
def find_correct_hand(opposing_hand: str, round_outcome: str) -> str:
if round_outcome == "draw":
return opposing_hand
if round_outcome == "win" and opposing_hand == "r":
return "p"
if round_outcome == "win" and opposing_hand == "p":
return "s"
if round_outcome == "win" and opposing_hand == "s":
return "r"
if round_outcome == "loss" and opposing_hand == "r":
return "s"
if round_outcome == "loss" and opposing_hand == "p":
return "r"
if round_outcome == "loss" and opposing_hand == "s":
return "p"
def get_round_score(hand: str, opposing_hand: str) -> int:
score = get_hand_score(hand)
score += get_outcome_score(get_round_outcome(hand, opposing_hand))
return score
def parse_encrypted_hands(line: str) -> tuple:
encrypted_opposing_hand = line[0]
encrypted_hand = line[2]
return (encrypted_hand, encrypted_opposing_hand)
def parse_encrypted_hands_and_outcomes(line: str) -> tuple:
encrypted_opposing_hand = line[0]
encrypted_outcome = line[2]
return (encrypted_opposing_hand, encrypted_outcome)
def decrypt_rounds(input_path: str = INPUT_PATH) -> list:
with open(input_path, "r") as input_data:
rounds = list()
for line in input_data:
encrypted_hands = parse_encrypted_hands(line)
rounds.append(
# (hand, opposing_hand)
(decrypt_hand(encrypted_hands[0]), (decrypt_hand(encrypted_hands[1])))
)
return rounds
def decrypt_opposing_hands_and_outcomes(input_path: str = INPUT_PATH) -> list:
with open(input_path, "r") as input_data:
opposing_hands_and_outcomes = list()
for line in input_data:
opposing_hand_and_outcome = parse_encrypted_hands_and_outcomes(line)
opposing_hands_and_outcomes.append(
# (opposing_hand, outcome)
(
decrypt_hand(opposing_hand_and_outcome[0]),
(decrypt_outcome(opposing_hand_and_outcome[1])),
)
)
return opposing_hands_and_outcomes
def get_total_score(rounds: list) -> int:
return sum([get_round_score(curr_round[0], curr_round[1]) for curr_round in rounds])
def get_correct_total_score(opposing_hands_and_outcomes: list) -> int:
score = 0
for opposing_hand_and_outcome in opposing_hands_and_outcomes:
opposing_hand = opposing_hand_and_outcome[0]
outcome = opposing_hand_and_outcome[1]
hand = find_correct_hand(opposing_hand, outcome)
score += get_outcome_score(outcome) + get_hand_score(hand)
return score
def main():
rounds = decrypt_rounds()
total_score = get_total_score(rounds)
opposing_hands_and_outcomes = decrypt_opposing_hands_and_outcomes()
correct_total_score = get_correct_total_score(opposing_hands_and_outcomes)
utils.write_answers_to_file(total_score, correct_total_score, file_name="answer_2.txt")
print(total_score, correct_total_score)
if __name__ == "__main__":
main()