Răsfoiți Sursa

Solve day 2

Taddeus Kroes 6 ani în urmă
părinte
comite
7856b0b2ff
2 a modificat fișierele cu 32 adăugiri și 0 ștergeri
  1. 31 0
      2019/02_intcode.py
  2. 1 0
      2019/input/2

+ 31 - 0
2019/02_intcode.py

@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+import sys
+from operator import add, mul
+
+def run(p):
+    pc = 0
+    opcode = p[pc]
+    while p[pc] != 99:
+        opcode, in1, in2, out = p[pc:pc + 4]
+        op = add if opcode == 1 else mul
+        p[out] = op(p[in1], p[in2])
+        pc += 4
+
+def initrun(p, noun, verb):
+    p = list(p)
+    p[1:3] = noun, verb
+    run(p)
+    return p[0]
+
+def find_params(p, desired_result):
+    noun = verb = 0
+    while initrun(p, noun, verb) <= desired_result:
+        noun += 1
+    noun -= 1
+    while initrun(p, noun, verb) < desired_result:
+        verb += 1
+    return 100 * noun + verb
+
+program = list(map(int, sys.stdin.read().split(',')))
+print(initrun(program, 12, 2))
+print(find_params(program, 19690720))

+ 1 - 0
2019/input/2

@@ -0,0 +1 @@
+1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,9,1,19,1,5,19,23,2,9,23,27,1,27,5,31,2,31,13,35,1,35,9,39,1,39,10,43,2,43,9,47,1,47,5,51,2,13,51,55,1,9,55,59,1,5,59,63,2,6,63,67,1,5,67,71,1,6,71,75,2,9,75,79,1,79,13,83,1,83,13,87,1,87,5,91,1,6,91,95,2,95,13,99,2,13,99,103,1,5,103,107,1,107,10,111,1,111,13,115,1,10,115,119,1,9,119,123,2,6,123,127,1,5,127,131,2,6,131,135,1,135,2,139,1,139,9,0,99,2,14,0,0