aoc

commit f68558d7fa5a8012000e94a25de463ee20f121e5

Author: Honza Pokorny <me@honza.ca>

Add 11p1

 011/input | 1 
 011/main.py | 390 +++++++++++++++++++++++++++++++++++++++++++++++++++++++


diff --git a/011/input b/011/input
new file mode 100644
index 0000000000000000000000000000000000000000..cdf4d55c3d83a12d74c00403f305d5efcb7bbd8d
--- /dev/null
+++ b/011/input
@@ -0,0 +1 @@
+3,8,1005,8,321,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,29,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,50,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,73,1,1105,16,10,2,1004,8,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,103,1006,0,18,1,105,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,131,1006,0,85,1,1008,0,10,1006,0,55,2,104,4,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,168,2,1101,1,10,1006,0,14,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,196,1006,0,87,1006,0,9,1,102,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,228,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,250,2,5,0,10,2,1009,9,10,2,107,17,10,1006,0,42,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,287,2,102,8,10,1006,0,73,1006,0,88,1006,0,21,101,1,9,9,1007,9,925,10,1005,10,15,99,109,643,104,0,104,1,21102,1,387353256856,1,21101,0,338,0,1105,1,442,21101,936332866452,0,1,21101,349,0,0,1105,1,442,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,179357024347,1,21101,0,396,0,1105,1,442,21102,1,29166144659,1,21102,407,1,0,1105,1,442,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,718170641252,1,21102,430,1,0,1106,0,442,21101,825012151040,0,1,21102,441,1,0,1106,0,442,99,109,2,21202,-1,1,1,21102,1,40,2,21102,1,473,3,21102,463,1,0,1105,1,506,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,468,469,484,4,0,1001,468,1,468,108,4,468,10,1006,10,500,1102,1,0,468,109,-2,2105,1,0,0,109,4,1202,-1,1,505,1207,-3,0,10,1006,10,523,21101,0,0,-3,22101,0,-3,1,21202,-2,1,2,21102,1,1,3,21102,1,542,0,1105,1,547,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,570,2207,-4,-2,10,1006,10,570,22102,1,-4,-4,1105,1,638,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,0,589,0,1106,0,547,22102,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,608,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,630,21202,-1,1,1,21102,630,1,0,105,1,505,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0
\ No newline at end of file




diff --git a/011/main.py b/011/main.py
new file mode 100644
index 0000000000000000000000000000000000000000..45adc445a2957ef1bb583b26706d7c517136a485
--- /dev/null
+++ b/011/main.py
@@ -0,0 +1,390 @@
+import re
+import copy
+import itertools
+
+opcode = re.compile(r'(\d{2})')
+
+OPS = ['01', '02', '03', '04', '99']
+NEEDS_INPUT = 'NI'
+HALT = 'H'
+
+
+def rev(s):
+    r = []
+    for l in s:
+        r.append(l)
+
+    r.reverse()
+    return ''.join(r)
+
+
+
+class NonHaltException(Exception):
+    pass
+
+
+def add(a, b):
+    return a + b
+
+
+def mult(a, b):
+    return a * b
+
+
+def intcode(input_list, *input_codes, cur=0):
+    input_list += [0 for _ in range(10_000)]
+    input_codes = list(input_codes)
+    input_codes.reverse()
+
+    outputs = []
+
+    # if input_list[0] == 3:
+    #     input_list[input_list[1]] = input_codes.pop()
+    #     cur += 2
+
+    rel = 0
+    while True:
+        op_value = input_list[cur]
+        op_value_string = '000000' + str(op_value)
+
+        op = op_value_string[-2:]
+
+        front = op_value_string[0:-2]
+        front = rev(front)
+
+        if op == '99':
+            break
+
+        if op == '01':
+            # 0 po, like before
+            # 1 im
+            a_mode = front[0]
+            b_mode = front[1]
+            c_mode = front[2]
+
+            if a_mode == '0':
+                a = input_list[input_list[cur + 1]]
+            elif a_mode == '1':
+                a = input_list[cur + 1]
+            elif a_mode == '2':
+                a = input_list[rel + input_list[cur + 1]]
+            else:
+                # relative
+                raise NotImplementedError
+
+            if b_mode == '0':
+                b = input_list[input_list[cur + 2]]
+            elif b_mode == '1':
+                b = input_list[cur + 2]
+            elif b_mode == '2':
+                b = input_list[rel + input_list[cur + 2]]
+            else:
+                raise NotImplementedError
+
+            if c_mode == '2':
+                result_pos = rel + input_list[cur + 3]
+            else:
+                result_pos = input_list[cur + 3]
+
+            input_list[result_pos] = add(a, b)
+            cur += 4
+            continue
+
+        if op == '02':
+            # 0 po, like before
+            # 1 im
+            a_mode = front[0]
+            b_mode = front[1]
+            c_mode = front[2]
+
+            if a_mode == '0':
+                a = input_list[input_list[cur + 1]]
+            elif a_mode == '1':
+                a = input_list[cur + 1]
+            elif a_mode == '2':
+                a = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            if b_mode == '0':
+                b = input_list[input_list[cur + 2]]
+            elif b_mode == '1':
+                b = input_list[cur + 2]
+            elif b_mode == '2':
+                b = input_list[rel + input_list[cur + 2]]
+            else:
+                raise NotImplementedError
+
+            if c_mode == '2':
+                result_pos = rel + input_list[cur + 3]
+            else:
+                result_pos = input_list[cur + 3]
+
+            input_list[result_pos] = mult(a, b)
+            cur += 4
+            continue
+
+        if op == '03':
+
+            if not input_codes:
+                return {
+                    'output': outputs,
+                    'state': NEEDS_INPUT,
+                    'memory': input_list,
+                    'cur': cur,
+                }
+
+            mode = front[0]
+
+            if mode == '0':
+                input_list[input_list[cur + 1]] = input_codes.pop()
+            elif mode == '1':
+                input_list[cur + 1] = input_codes.pop()
+            elif mode == '2':
+                input_list[rel + input_list[cur + 1]] = input_codes.pop()
+            else:
+                raise NotImplementedError
+
+            # input_list[input_list[cur + 1]] = input_codes.pop()
+            cur += 2
+            continue
+
+        if op == '04':
+            mode = front[0]
+            if mode == '0':
+                o = input_list[input_list[cur + 1]]
+            elif mode == '1':
+                o = input_list[cur + 1]
+            elif mode == '2':
+                o = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            outputs.append(o)
+            cur += 2
+            continue
+
+        if op == '05':
+            mode = front[0]
+            if mode == '0':
+                v = input_list[input_list[cur + 1]]
+            elif mode == '1':
+                v = input_list[cur + 1]
+            elif mode == '2':
+                v = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            if v is not 0:
+                m2 = front[1]
+                if m2 == '0':
+                    cur = input_list[input_list[cur + 2]]
+                elif m2 == '1':
+                    cur = input_list[cur + 2]
+                elif m2 == '2':
+                    cur = input_list[rel + input_list[cur + 2]]
+                else:
+                    raise NotImplementedError
+            else:
+                cur += 3
+
+        if op == '06':
+            mode = front[0]
+            if mode == '0':
+                v = input_list[input_list[cur + 1]]
+            elif mode == '1':
+                v = input_list[cur + 1]
+            elif mode == '2':
+                v = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            if v == 0:
+                m2 = front[1]
+                if m2 == '0':
+                    cur = input_list[input_list[cur + 2]]
+                elif m2 == '1':
+                    cur = input_list[cur + 2]
+                elif m2 == '2':
+                    cur = input_list[rel + input_list[cur + 2]]
+                else:
+                    raise NotImplementedError
+            else:
+                cur += 3
+
+        if op == '07':
+            # 0 po, like before
+            # 1 im
+            a_mode = front[0]
+            b_mode = front[1]
+            c_mode = front[2]
+
+            if a_mode == '0':
+                a = input_list[input_list[cur + 1]]
+            elif a_mode == '1':
+                a = input_list[cur + 1]
+            elif a_mode == '2':
+                a = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            if b_mode == '0':
+                b = input_list[input_list[cur + 2]]
+            elif b_mode == '1':
+                b = input_list[cur + 2]
+            elif b_mode == '2':
+                b = input_list[rel + input_list[cur + 2]]
+            else:
+                raise NotImplementedError
+
+            if c_mode == '2':
+                result_pos = rel + input_list[cur + 3]
+            else:
+                result_pos = input_list[cur + 3]
+
+            if a < b:
+                input_list[result_pos] = 1
+            else:
+                input_list[result_pos] = 0
+
+            cur += 4
+            continue
+
+        if op == '08':
+            # 0 po, like before
+            # 1 im
+            a_mode = front[0]
+            b_mode = front[1]
+            c_mode = front[2]
+
+            if a_mode == '0':
+                a = input_list[input_list[cur + 1]]
+            elif a_mode == '1':
+                a = input_list[cur + 1]
+            elif a_mode == '2':
+                a = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            if b_mode == '0':
+                b = input_list[input_list[cur + 2]]
+            elif b_mode == '1':
+                b = input_list[cur + 2]
+            elif b_mode == '2':
+                b = input_list[rel + input_list[cur + 2]]
+            else:
+                raise NotImplementedError
+
+            if c_mode == '2':
+                result_pos = rel + input_list[cur + 3]
+            else:
+                result_pos = input_list[cur + 3]
+
+            if a == b:
+                input_list[result_pos] = 1
+            else:
+                input_list[result_pos] = 0
+
+            cur += 4
+            continue
+
+        if op == '09':
+            mode = front[0]
+            if mode == '0':
+                v = input_list[input_list[cur + 1]]
+            elif mode == '1':
+                v = input_list[cur + 1]
+            elif mode == '2':
+                v = input_list[rel + input_list[cur + 1]]
+            else:
+                raise NotImplementedError
+
+            rel += v
+            cur += 2
+
+    return {
+        'output': outputs,
+        'state': HALT,
+        'memory': input_list,
+        'cur': None
+    }
+
+
+def new_direction(current_direction, turn):
+    if turn == 0:  # left
+        if current_direction == 0:
+            return 3
+        else:
+            return current_direction - 1
+    if turn == 1:
+        if current_direction == 3:
+            return 0
+        else:
+            return current_direction + 1
+
+
+def new_position(current_position, direction):
+    x, y = current_position
+
+    if direction == 0:
+        return (x, y + 1)
+
+    if direction == 1:
+        return (x + 1, y)
+
+    if direction == 2:
+        return (x, y - 1)
+
+    if direction == 3:
+        return (x -1, y)
+
+    raise Exception('wrong direction')
+
+
+def main():
+    data = open('input').read().split(',')
+    data = list(map(int, data))
+
+    current_position = (0, 0)
+    current_direction = 0
+    memory = copy.deepcopy(data)
+    a = {}
+    cur = 0
+
+    # 0 up
+    # 1 right
+    # 2 down
+    # 3 left
+    # direction = 0
+
+
+
+    # read color
+    # say what to paint
+    # turn
+    # step
+
+    while True:
+        print(current_position)
+        color = a.get(current_position, 0)
+
+        out = intcode(memory, color, cur=cur)
+
+        if out['state'] == HALT:
+            break
+
+        color, turn = out['output']
+
+        a[current_position] = color
+        current_direction = new_direction(current_direction, turn)
+        current_position = new_position(current_position, current_direction)
+        memory = out['memory']
+        cur = out['cur']
+
+    keys = set(list(a.keys()))
+    print(len(keys))
+
+
+
+if __name__ == '__main__':
+    main()