This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_balance_bots.py
67 lines (50 loc) · 1.63 KB
/
10_balance_bots.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
################################
# --- Day 10: Balance Bots --- #
################################
import AOCUtils
class Bot:
def __init__(self):
self.chips = []
self.lowType, self.lowID = None, None
self.highType, self.highID = None, None
################################
instructions = AOCUtils.loadInput(10)
bots = dict()
for inst in instructions:
inst = inst.split()
if inst[0] == "bot":
botID = int(inst[1])
if botID not in bots:
bots[botID] = Bot()
bots[botID].lowType = inst[5]
bots[botID].lowID = int(inst[6])
bots[botID].highType = inst[10]
bots[botID].highID = int(inst[11])
elif inst[0] == "value":
botID = int(inst[5])
if botID not in bots:
bots[botID] = Bot()
chip = int(inst[1])
bots[botID].chips.append(chip)
output = dict()
activeBots = [botID for botID, bot in bots.items() if len(bot.chips) == 2]
while activeBots:
botID = activeBots.pop()
bot = bots[botID]
lowChip, highChip = sorted(bot.chips)
if [lowChip, highChip] == [17, 61]:
p1 = botID
low = (bot.lowType, bot.lowID, lowChip)
high = (bot.highType, bot.highID, highChip)
for tgtType, tgtID, chip in [low, high]:
if tgtType == "bot":
bots[tgtID].chips.append(chip)
elif tgtType == "output":
output[tgtID] = chip
if tgtType == "bot" and len(bots[tgtID].chips) == 2:
activeBots.append(tgtID)
bot.chips = []
print("Part 1: {}".format(p1))
p2 = output[0] * output[1] * output[2]
print("Part 2: {}".format(p2))
AOCUtils.printTimeTaken()