-
Notifications
You must be signed in to change notification settings - Fork 0
/
easter_101.py
65 lines (56 loc) · 1.81 KB
/
easter_101.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re,weakref,itertools
_input = 'easter_10.input'
bots = []
bins = []
class robot:
bots = weakref.WeakValueDictionary()
def __init__(self, name, low, high ):
self.name = name
self.low_id = low
self.high_id = high
self.A = None
self.B = None
self.bots[self.name] = self
def load_chip(self,value):
if not self.A:
print("Bot {} is loading first chip ({})".format(self.name,value))
self.A = value
elif not self.B:
print("Bot {} is loading second chip ({})".format(self.name,value))
self.B = value
self.unload_chips()
else:
raise Exception("Bot {} is loading a third chip".format(self.name))
def unload_chips(self):
print("Bot {} is giving chips {} and {}".format(self.name,self.A,self.B))
low = min(self.A,self.B)
high = max(self.A,self.B)
if ((low == 17) and (high == 61)):
print( "We found it : {}".format(self.name))
quit()
if self.low_id:
robot.bots[self.low_id].load_chip( low )
if self.high_id:
robot.bots[self.high_id].load_chip( high )
self.A = None
self.B = None
def read_file(input_file):
# We don't care about output bins
_re_bot = re.compile('^bot (\d+) gives low to (?:output \d+|bot (\d+)) and high to (?:output \d+|bot (\d+))$')
_re_bin = re.compile('^value (\d+) goes to bot (\d+)$')
with open(input_file) as f:
for line in f:
line = line.rstrip()
bot = _re_bot.match(line)
bin = _re_bin.match(line)
if bot:
bots.append( robot(bot[1],bot[2],bot[3]))
elif bin:
bins.append([bin[2],int(bin[1])])
else:
raise Exception ("Unable to interpret line : {}".format(line))
read_file(_input)
for input in itertools.cycle(bins):
robot.bots[input[0]].load_chip(input[1])