Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import product
- import numpy as np
- from tqdm import tqdm
- def run_prog(prog, inputs):
- assert len(inputs) == 14
- v = dict(w=0, x=0, y=0, z=0)
- input_ind = 0
- for line in prog:
- parts = line.split()
- op = parts[0]
- args = parts[1:]
- if op == 'inp':
- v[args[0]] = inputs[input_ind]
- input_ind += 1
- elif op == 'add':
- arg0 = args[0]
- arg1 = v[args[1]] if args[1] in v else int(args[1])
- v[arg0] = v[arg0] + arg1
- elif op == 'mul':
- arg0 = args[0]
- arg1 = v[args[1]] if args[1] in v else int(args[1])
- v[arg0] = v[arg0] * arg1
- elif op == 'div':
- arg0 = args[0]
- arg1 = v[args[1]] if args[1] in v else int(args[1])
- v[arg0] = v[arg0] // arg1
- elif op == 'mod':
- arg0 = args[0]
- arg1 = v[args[1]] if args[1] in v else int(args[1])
- v[arg0] = v[arg0] % arg1
- elif op == 'eql':
- arg0 = args[0]
- arg1 = v[args[1]] if args[1] in v else int(args[1])
- if arg0 == 'x' and arg1 == -1:
- x = v[arg0]
- if x >= 1 and x <= 9:
- v['w'] = x
- inputs[input_ind - 1] = x
- arg1 = x
- else:
- return v, inputs
- v[arg0] = 1 if v[arg0] == arg1 else 0
- elif op == 'print':
- print(v)
- else:
- raise Exception(f'Unknown op {op}')
- return v, inputs
- prog = open('data/day24.txt').read().strip().splitlines()
- for i in tqdm(product(range(9, 0, -1), repeat=7), total=9**7):
- full_input = [i[0], i[1], i[2], i[3], -1, i[4], -1, i[5], -1, -1, i[6], -1, -1, -1]
- res, inp = run_prog(prog, full_input)
- if res['z'] == 0 and -1 not in inp:
- print(f'largest = {"".join([str(i) for i in inp])}')
- break
- for i in tqdm(product(range(1, 10), repeat=7), total=9**7):
- full_input = [i[0], i[1], i[2], i[3], -1, i[4], -1, i[5], -1, -1, i[6], -1, -1, -1]
- res, inp = run_prog(prog, full_input)
- if res['z'] == 0 and -1 not in inp:
- print(f'smallest = {"".join([str(i) for i in inp])}')
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement