-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol22.py
138 lines (97 loc) · 3.93 KB
/
sol22.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
130
131
132
133
134
135
136
137
138
import utils
import copy
def initialise():
global playerOneDeck, playerTwoDeck
deck = utils.loadInputFile("input_22.txt")
for idx, card in enumerate(deck):
if card == "":
break
playerOneDeck = deck[1:idx]
playerTwoDeck = deck[idx+2:]
playerOneDeck = [int(x) for x in playerOneDeck]
playerTwoDeck = [int(x) for x in playerTwoDeck]
assert(len(playerOneDeck) == len(playerTwoDeck))
def subGame(playerOneDeck, playerTwoDeck):
deckHistory = []
playerOneDeck = copy.deepcopy(playerOneDeck)
playerTwoDeck = copy.deepcopy(playerTwoDeck)
while len(playerOneDeck) > 0 and len(playerTwoDeck) > 0:
currentDeckStatus = playerOneDeck + ["|"] + playerTwoDeck
if currentDeckStatus in deckHistory:
# defaults to player one winning
break
else:
deckHistory.append(currentDeckStatus)
playerOneCard = playerOneDeck.pop(0)
playerTwoCard = playerTwoDeck.pop(0)
if len(playerOneDeck) >= playerOneCard and len(playerTwoDeck) >= playerTwoCard:
if subGame(playerOneDeck[:playerOneCard], playerTwoDeck[:playerTwoCard]):
playerOneDeck.append(playerOneCard)
playerOneDeck.append(playerTwoCard)
else:
playerTwoDeck.append(playerTwoCard)
playerTwoDeck.append(playerOneCard)
elif playerOneCard > playerTwoCard:
playerOneDeck.append(playerOneCard)
playerOneDeck.append(playerTwoCard)
elif playerTwoCard > playerOneCard:
playerTwoDeck.append(playerTwoCard)
playerTwoDeck.append(playerOneCard)
else:
raise Exception
# return True if player one wins
return len(playerOneDeck) > 0
def processRound(playerOneDeck, playerTwoDeck):
playerOneCard = playerOneDeck.pop(0)
playerTwoCard = playerTwoDeck.pop(0)
if playerOneCard > playerTwoCard:
playerOneDeck.append(playerOneCard)
playerOneDeck.append(playerTwoCard)
elif playerTwoCard > playerOneCard:
playerTwoDeck.append(playerTwoCard)
playerTwoDeck.append(playerOneCard)
else:
raise Exception
def processRecursiveRound(playerOneDeck, playerTwoDeck):
playerOneCard = playerOneDeck.pop(0)
playerTwoCard = playerTwoDeck.pop(0)
if playerOneCard == 4 and playerTwoCard == 3:
print("popping: ", playerOneCard, playerTwoCard)
if len(playerOneDeck) >= playerOneCard and len(playerTwoDeck) >= playerTwoCard:
if subGame(playerOneDeck[:playerOneCard], playerTwoDeck[:playerTwoCard]):
playerOneDeck.append(playerOneCard)
playerOneDeck.append(playerTwoCard)
else:
playerTwoDeck.append(playerTwoCard)
playerTwoDeck.append(playerOneCard)
elif playerOneCard > playerTwoCard:
playerOneDeck.append(playerOneCard)
playerOneDeck.append(playerTwoCard)
elif playerTwoCard > playerOneCard:
playerTwoDeck.append(playerTwoCard)
playerTwoDeck.append(playerOneCard)
else:
raise Exception
def calculateScore(playerOneDeck, playerTwoDeck):
score = 0
if len(playerOneDeck) > 0:
for idx, card in enumerate(playerOneDeck):
score += (card * (len(playerOneDeck) - idx))
else:
for idx, card in enumerate(playerTwoDeck):
score += (card * (len(playerTwoDeck) - idx))
return score
def part1():
global playerOneDeck, playerTwoDeck
while len(playerOneDeck) > 0 and len(playerTwoDeck) > 0:
processRound(playerOneDeck, playerTwoDeck)
return calculateScore(playerOneDeck, playerTwoDeck)
def part2():
global playerOneDeck, playerTwoDeck
while len(playerOneDeck) > 0 and len(playerTwoDeck) > 0:
processRecursiveRound(playerOneDeck, playerTwoDeck)
return calculateScore(playerOneDeck, playerTwoDeck)
initialise()
print("Part 1:", part1()) # 32366
initialise()
print("Part 2:", part2()) # 30891