robots finished

This commit is contained in:
jfmontanaro 2017-01-18 15:16:47 -08:00
parent d54d5c7a16
commit 0edd2a33b8

43
10-1.py
View File

@ -1,7 +1,7 @@
# Python! # Python!
import collections import collections
class bot: class robot:
def __init__(self, instructions): def __init__(self, instructions):
words = instructions.split() words = instructions.split()
self.number = words[1] self.number = words[1]
@ -21,23 +21,22 @@ class bot:
def take(self, value): def take(self, value):
self.chips.append(value) self.chips.append(value)
if len(self.chips) > 1: if len(self.chips) > 1:
self.execute() xqueue.append(self)
def execute(self): def execute(self):
low, high = min(self.chips), max(self.chips) if len(self.chips) > 2:
print('Error, bot', self.number, 'has chips', self.chips)
elif len(self.chips) == 2:
low, high = min(self.chips), max(self.chips)
if low == '17' and high == '61': if low == 17 and high == 61:
print('responsible bot is bot', self.number) print('responsible bot is bot', self.number)
print('17 goes to', self.recipients['low'])
print('61 goes to', self.recipients['high'])
del self.chips[:] del self.chips[:]
self.give(low, self.recipients['low']) self.give(low, self.recipients['low'])
self.give(high, self.recipients['high']) 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: with open('10.txt') as file:
@ -45,8 +44,9 @@ with open('10.txt') as file:
get_instructions = [] get_instructions = []
bot_definitions = [] bot_definitions = []
outputs = collections.defaultdict(list) xqueue = []
bots = {} bots = {}
outputs = collections.defaultdict(list)
for inst in instructions: for inst in instructions:
if inst[0] == 'v': if inst[0] == 'v':
@ -55,8 +55,17 @@ for inst in instructions:
bot_definitions.append(inst) bot_definitions.append(inst)
for definition in bot_definitions: for definition in bot_definitions:
b = bot(definition) b = robot(definition)
bots[b.number] = b bots[b.number] = b
for i in get_instructions: for i in get_instructions:
handle_instruction(i) words = i.split()
bot_num = words[5]
value = int(words[1])
bots[bot_num].chips.append(value)
bots['97'].execute()
while xqueue:
current_bot = xqueue.pop(0)
current_bot.execute()