-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartTwo.py
More file actions
73 lines (53 loc) · 1.87 KB
/
Copy pathpartTwo.py
File metadata and controls
73 lines (53 loc) · 1.87 KB
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
from common import *
import copy
def play_round(
deck_one: List[int], deck_two: List[int]
) -> Tuple[int, Tuple[List[int], List[int]]]:
# returns winner number and decks in their new state
# print("new round")
seen_configurations = []
while len(deck_one) > 0 and len(deck_two) > 0:
current_hash = hash(tuple(deck_one + deck_two))
if current_hash in seen_configurations:
# print("Seen this config before")
return 1, (deck_one, deck_two)
else:
seen_configurations.append(current_hash)
# do gameplay shenanigans
top_one = deck_one.pop(0)
top_two = deck_two.pop(0)
winner = 0
if len(deck_one) >= top_one and len(deck_two) >= top_two:
# recurse! 🦀
# print("recursing 🦀")
new_deck_one = deck_one[:top_one]
new_deck_two = deck_two[:top_two]
winner, _ = play_round(new_deck_one, new_deck_two)
else:
# play as usual
if top_one > top_two:
winner = 1
elif top_two > top_one:
winner = 2
# print(winner, "wins")
# do things based on the winner
if winner == 1:
deck_one.append(top_one)
deck_one.append(top_two)
elif winner == 2:
deck_two.append(top_two)
deck_two.append(top_one)
else:
raise Exception("SDFGKSHDFGKSHDFGAAAAAAAA!!!") # yes
# print("finished this round")
return 1 if len(deck_one) != 0 else 2, (deck_one, deck_two)
def partTwo(instr: str) -> int:
deck_one, deck_two = parse(instr)
winner, (deck_one, deck_two) = play_round(
copy.deepcopy(deck_one), copy.deepcopy(deck_two)
)
if winner == 1:
return calc_score(deck_one)
elif winner == 2:
return calc_score(deck_two)
return 0