-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle21_part1.py
73 lines (56 loc) · 2.16 KB
/
puzzle21_part1.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
66
67
68
69
70
71
72
73
from intcode import IntCodeComputer
from itertools import product
def _not(x, y):
return not x
def _and(x, y):
return x and y
def _or(x, y):
return x or y
def run_simulation(springscript, abcd_dict):
local_vars = {'j': False, 't': False}
local_vars.update(abcd_dict)
for op, var1, var2 in springscript:
local_vars[var2] = op(local_vars[var1], local_vars[var2])
return local_vars['j']
test_script = [(_not, 'a', 'j'),
(_not, 'b', 't'),
(_and, 't', 'j'),
(_not, 'c', 't'),
(_and, 't', 'j'),
(_and, 'd', 'j')]
assert run_simulation(test_script, dict(zip('abcd', [False, False, False, True])))
# configuration where drone should jump
should_jump = [(False, False, False, True),
(True, True, False, True),
(True, False, True, True),
(False, True, True, True)]
# configurations where drone should not jump
should_not_jump = [(True, True, False, False),
[True, True, True, True],
[True, False, False, False]]
all_ops = list(product([_not, _and, _or], 'abcdjt', 'jt'))
found = False
for script_len in range(1, 16):
print(f'starting script_len {script_len}')
for script in product(all_ops, repeat=script_len):
for abcd, expected in zip(should_jump + should_not_jump,
[True] * len(should_jump) + [False] * len(should_not_jump)):
if run_simulation(script, dict(zip('abcd', abcd))) is not expected:
break
else:
print('Found a solution script!')
found = True
break
if found: break
input_map = {_and: 'AND', _or: 'OR', _not: 'NOT'}
out = ""
for op, var1, var2 in script:
out += " ".join([input_map[op], var1.upper(), var2.upper()]) + '\n'
out += 'WALK\n'
print(out)
out_ascii = [ord(char) for char in out]
program = list(map(int, open('data/input21').read().strip().split(',')))
computer = IntCodeComputer(program, resume=False)
_, prints, status = computer.run(input_values=out_ascii)
message = "".join([chr(p) for p in prints if p < 128])
print(f"solution for part1: {prints[-1]}")