62 lines
1.2 KiB
Python
62 lines
1.2 KiB
Python
|
# Python!
|
||
|
import collections
|
||
|
|
||
|
class bot:
|
||
|
def __init__(self, instructions):
|
||
|
words = instructions.split()
|
||
|
self.number = words[1]
|
||
|
self.chips = []
|
||
|
self.recipients = {
|
||
|
'low': words[5:7],
|
||
|
'high': words[10:]
|
||
|
}
|
||
|
|
||
|
def give(self, value, recipient):
|
||
|
recip_type, recip_num = recipient
|
||
|
if recip_type == 'output':
|
||
|
outputs[recip_num].append(value)
|
||
|
else:
|
||
|
bots[recip_num].take(value)
|
||
|
|
||
|
def take(self, value):
|
||
|
self.chips.append(value)
|
||
|
if len(self.chips) > 1:
|
||
|
self.execute()
|
||
|
|
||
|
def execute(self):
|
||
|
low, high = min(self.chips), max(self.chips)
|
||
|
|
||
|
if low == '17' and high == '61':
|
||
|
print('responsible bot is bot', self.number)
|
||
|
|
||
|
del self.chips[:]
|
||
|
self.give(low, self.recipients['low'])
|
||
|
self.give(high, self.recipients['high'])
|
||
|
|
||
|
def handle_instruction(inst):
|
||
|
words = i.split()
|
||
|
bot_num = words[5]
|
||
|
value = words[1]
|
||
|
bots[bot_num].take(value)
|
||
|
|
||
|
|
||
|
with open('10.txt') as file:
|
||
|
instructions = file.read().splitlines()
|
||
|
|
||
|
get_instructions = []
|
||
|
bot_definitions = []
|
||
|
outputs = collections.defaultdict(list)
|
||
|
bots = {}
|
||
|
|
||
|
for inst in instructions:
|
||
|
if inst[0] == 'v':
|
||
|
get_instructions.append(inst)
|
||
|
else:
|
||
|
bot_definitions.append(inst)
|
||
|
|
||
|
for definition in bot_definitions:
|
||
|
b = bot(definition)
|
||
|
bots[b.number] = b
|
||
|
|
||
|
for i in get_instructions:
|
||
|
handle_instruction(i)
|