Advertisement
Guest User

Untitled

a guest
Dec 24th, 2021
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. from itertools import product
  2. import numpy as np
  3. from tqdm import tqdm
  4.  
  5.  
  6. def run_prog(prog, inputs):
  7.     assert len(inputs) == 14
  8.  
  9.     v = dict(w=0, x=0, y=0, z=0)
  10.     input_ind = 0
  11.  
  12.     for line in prog:
  13.         parts = line.split()
  14.         op = parts[0]
  15.         args = parts[1:]
  16.  
  17.         if op == 'inp':
  18.             v[args[0]] = inputs[input_ind]
  19.             input_ind += 1
  20.         elif op == 'add':
  21.             arg0 = args[0]
  22.             arg1 = v[args[1]] if args[1] in v else int(args[1])
  23.  
  24.             v[arg0] = v[arg0] + arg1
  25.         elif op == 'mul':
  26.             arg0 = args[0]
  27.             arg1 = v[args[1]] if args[1] in v else int(args[1])
  28.  
  29.             v[arg0] = v[arg0] * arg1
  30.         elif op == 'div':
  31.             arg0 = args[0]
  32.             arg1 = v[args[1]] if args[1] in v else int(args[1])
  33.  
  34.             v[arg0] = v[arg0] // arg1
  35.         elif op == 'mod':
  36.             arg0 = args[0]
  37.             arg1 = v[args[1]] if args[1] in v else int(args[1])
  38.  
  39.             v[arg0] = v[arg0] % arg1
  40.         elif op == 'eql':
  41.             arg0 = args[0]
  42.             arg1 = v[args[1]] if args[1] in v else int(args[1])
  43.  
  44.             if arg0 == 'x' and arg1 == -1:
  45.                 x = v[arg0]
  46.                 if x >= 1 and x <= 9:
  47.                     v['w'] = x
  48.                     inputs[input_ind - 1] = x
  49.                     arg1 = x
  50.                 else:
  51.                     return v, inputs
  52.  
  53.             v[arg0] = 1 if v[arg0] == arg1 else 0
  54.         elif op == 'print':
  55.             print(v)
  56.         else:
  57.             raise Exception(f'Unknown op {op}')
  58.  
  59.     return v, inputs
  60.  
  61.  
  62. prog = open('data/day24.txt').read().strip().splitlines()
  63.  
  64. for i in tqdm(product(range(9, 0, -1), repeat=7), total=9**7):
  65.     full_input = [i[0], i[1], i[2], i[3], -1, i[4], -1, i[5], -1, -1, i[6], -1, -1, -1]
  66.     res, inp = run_prog(prog, full_input)
  67.  
  68.     if res['z'] == 0 and -1 not in inp:
  69.         print(f'largest = {"".join([str(i) for i in inp])}')
  70.         break
  71.  
  72. for i in tqdm(product(range(1, 10), repeat=7), total=9**7):
  73.     full_input = [i[0], i[1], i[2], i[3], -1, i[4], -1, i[5], -1, -1, i[6], -1, -1, -1]
  74.     res, inp = run_prog(prog, full_input)
  75.  
  76.     if res['z'] == 0 and -1 not in inp:
  77.         print(f'smallest = {"".join([str(i) for i in inp])}')
  78.         break
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement