Day 16
parent
2845c51cac
commit
a32de1d995
@ -0,0 +1,37 @@
|
||||
from ops import ops
|
||||
from copy import copy
|
||||
|
||||
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)
|
||||
|
||||
res = 0
|
||||
|
||||
for sample in samples:
|
||||
count = 0
|
||||
num, a, b, c = sample['op']
|
||||
for op in ops:
|
||||
try:
|
||||
if sample['out'] == op(copy(sample['in']), a, b, c):
|
||||
count += 1
|
||||
except:
|
||||
pass
|
||||
if count >= 3:
|
||||
res += 1
|
||||
# print(sample)
|
||||
|
||||
print(res)
|
@ -0,0 +1,72 @@
|
||||
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])
|
@ -0,0 +1,5 @@
|
||||
Before: [3, 2, 1, 1]
|
||||
9 2 1 2
|
||||
After: [3, 2, 2, 1]
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,73 @@
|
||||
def addr(reg, a, b, c):
|
||||
reg[c] = reg[a] + reg[b]
|
||||
return reg
|
||||
|
||||
def addi(reg, a, b, c):
|
||||
reg[c] = reg[a] + b
|
||||
return reg
|
||||
|
||||
def mulr(reg, a, b, c):
|
||||
reg[c] = reg[a] * reg[b]
|
||||
return reg
|
||||
|
||||
def muli(reg, a, b, c):
|
||||
reg[c] = reg[a] * b
|
||||
return reg
|
||||
|
||||
def banr(reg, a, b, c):
|
||||
reg[c] = reg[a] & reg[b]
|
||||
return reg
|
||||
|
||||
def bani(reg, a, b, c):
|
||||
reg[c] = reg[a] & b
|
||||
return reg
|
||||
|
||||
def borr(reg, a, b, c):
|
||||
reg[c] = reg[a] | reg[b]
|
||||
return reg
|
||||
|
||||
def bori(reg, a, b, c):
|
||||
reg[c] = reg[a] | b
|
||||
return reg
|
||||
|
||||
def setr(reg, a, b, c):
|
||||
reg[c] = reg[a]
|
||||
return reg
|
||||
|
||||
def seti(reg, a, b, c):
|
||||
reg[c] = a
|
||||
return reg
|
||||
|
||||
def gtir(reg, a, b, c):
|
||||
reg[c] = 1 if a > reg[b] else 0
|
||||
return reg
|
||||
|
||||
def gtri(reg, a, b, c):
|
||||
reg[c] = 1 if reg[a] > b else 0
|
||||
return reg
|
||||
|
||||
def gtrr(reg, a, b, c):
|
||||
reg[c] = 1 if reg[a] > reg[b] else 0
|
||||
return reg
|
||||
|
||||
def eqir(reg, a, b, c):
|
||||
reg[c] = 1 if a == reg[b] else 0
|
||||
return reg
|
||||
|
||||
def eqri(reg, a, b, c):
|
||||
reg[c] = 1 if reg[a] == b else 0
|
||||
return reg
|
||||
|
||||
def eqrr(reg, a, b, c):
|
||||
reg[c] = 1 if reg[a] == reg[b] else 0
|
||||
return reg
|
||||
|
||||
ops = [
|
||||
addr, addi,
|
||||
mulr, muli,
|
||||
banr, bani,
|
||||
borr, bori,
|
||||
setr, seti,
|
||||
gtir, gtri, gtrr,
|
||||
eqir, eqri, eqrr
|
||||
]
|
Loading…
Reference in New Issue