repl.it
@21natzil/

Day 2

Python

Intcode Computer

fork
loading
Files
  • main.py
  • program.txt
main.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
with open("program.txt") as f:
  machine = [*map(int, f.read().split(','))]


def run_machine(inp1, inp2, machine):
  machine = machine[:]
  machine[1] = inp1
  machine[2] = inp2

  for i in range(0, len(machine), 4):
    opcode = machine[i]
    if opcode == 1:
      total = machine[machine[i+1]] + machine[machine[i+2]]
    elif opcode == 2:
      total = machine[machine[i+1]] * machine[machine[i+2]]
    elif opcode == 99:
      break
    else:
      break
    machine[machine[i+3]] = total
  return machine[0]

print("Part 1:", run_machine(12, 2, machine))
for x in range(0, 100):
  for y in range(0, 100):
    result = run_machine(x, y, machine)
    if result == 19690720:
      print("Part 2:", 100 * x + y)