-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_bots.py
60 lines (51 loc) · 1.98 KB
/
10_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
from collections import defaultdict
from functools import reduce
def read_commands(f):
commands = []
bots = defaultdict(list)
for row in f:
splitted = row.strip().split(' ')
if splitted[0] == 'value':
if splitted[-1] in bots.keys():
bots[splitted[-1]].append(int(splitted[1]))
bots[splitted[-1]].sort()
else:
bots[splitted[-1]] = [int(splitted[1])]
else:
commands.append([int(splitted[1]), int(splitted[6]), splitted[5], int(splitted[-1]), splitted[-2]])
return bots, commands
def check_bots(bots):
for key, value in bots.items():
if value == [17,61]: print('Bot {0} has {1}'.format(key, value))
def execute_commands(bots, commands):
outputs = defaultdict(list)
idx = 0
while len(commands) > 0:
cmd = commands[idx]
if len(bots[str(cmd[0])]) == 2 and (cmd[2] == 'output' or len(bots[str(cmd[1])]) < 2) and (cmd[4] == 'output' or len(bots[str(cmd[3])]) < 2):
tmp = bots[str(cmd[0])]
bots[str(cmd[0])] = []
if cmd[2] == 'bot':
bots[str(cmd[1])].append(tmp[0])
bots[str(cmd[1])].sort()
else:
outputs[str(cmd[1])].append(tmp[0])
if cmd[4] == 'bot':
bots[str(cmd[3])].append(tmp[1])
bots[str(cmd[3])].sort()
else:
outputs[str(cmd[3])].append(tmp[1])
commands.pop(idx)
idx = 0
check_bots(bots)
continue
idx += 1
if idx > len(commands):
print('Error ! Can\'t execute all commands !')
return None, None
return bots, outputs
if __name__ == '__main__':
bots, commands = read_commands(open('data/10_bots.data'))
check_bots(bots)
bots, outputs = execute_commands(bots, commands)
print('Result for second part is : {0}'.format(reduce(lambda x,y: x*y,[outputs[str(x)][0] for x in range(3)],1)))