You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.6 KiB
Python
72 lines
1.6 KiB
Python
6 years ago
|
from ops import ops
|
||
|
from copy import copy
|
||
|
from pprint import pprint
|
||
|
|
||
|
with open('input.txt', 'r') as f:
|
||
|
raw = f.readlines()
|
||
|
|
||
|
samples = []
|
||
|
i = 0
|
||
|
while 1:
|
||
|
sample = {}
|
||
|
if raw[i] == '\n':
|
||
|
break
|
||
|
|
||
|
sample['in'] = [int(x) for x in raw[i][9:-2].split(', ')]
|
||
|
sample['op'] = [int(x) for x in raw[i + 1].strip().split(' ')]
|
||
|
sample['out'] = [int(x) for x in raw[i + 2][9:-2].split(', ')]
|
||
|
samples.append(sample)
|
||
|
i += 4
|
||
|
|
||
|
# print(samples)
|
||
|
|
||
|
observations = dict()
|
||
|
|
||
|
for sample in samples:
|
||
|
count = 0
|
||
|
num, a, b, c = sample['op']
|
||
|
valid = set()
|
||
|
for op in ops:
|
||
|
try:
|
||
|
if sample['out'] == op(copy(sample['in']), a, b, c):
|
||
|
count += 1
|
||
|
valid.add(op)
|
||
|
except:
|
||
|
pass
|
||
|
|
||
|
if observations.get(num) is None:
|
||
|
observations[num] = valid
|
||
|
else:
|
||
|
observations[num] = observations[num].intersection(valid)
|
||
|
|
||
|
# pprint(observations)
|
||
|
ops_map = dict()
|
||
|
|
||
|
while len(observations) != 0:
|
||
|
to_del = list()
|
||
|
for num, funcs in observations.items():
|
||
|
funcs = funcs - set(ops_map.values())
|
||
|
if len(funcs) == 1:
|
||
|
ops_map[num] = next(iter(funcs))
|
||
|
to_del.append(num)
|
||
|
for num in to_del:
|
||
|
del observations[num]
|
||
|
|
||
|
pprint(ops_map)
|
||
|
|
||
|
with open('input.txt', 'r') as f:
|
||
|
raw = f.readlines()
|
||
|
instructions = []
|
||
|
for line in raw[i + 2:]:
|
||
|
instructions.append([int(x) for x in line.strip().split(' ')])
|
||
|
|
||
|
pprint(instructions)
|
||
|
|
||
|
reg = [0, 0, 0, 0]
|
||
|
|
||
|
for ins in instructions:
|
||
|
num, a, b, c = ins
|
||
|
reg = ops_map[num](reg, a, b, c)
|
||
|
|
||
|
print(reg)
|
||
|
print(reg[0])
|