aoc

ref: 6f8d4d6961731dd934f63132d520ce112165f291

002/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
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
74
75
import copy


OPS = [1, 2, 99]


class NonHaltException(Exception):
    pass


def add(a, b):
    return a + b


def mult(a, b):
    return a * b


def intcode(input_list, a, b):
    # fix it
    input_list[1] = a
    input_list[2] = b

    cur = 0

    while True:
        op = input_list[cur]

        if op not in OPS:
            raise NonHaltException

        if op == 99:
            break

        f = None

        if op == 1:
            f = add

        if op == 2:
            f = mult

        if not f:
            continue

        a, b = input_list[cur + 1], input_list[cur + 2]
        result_pos = input_list[cur + 3]
        input_list[result_pos] = f(input_list[a], input_list[b])
        cur += 4

    return input_list[0]


def main():

    input_list = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,2,6,19,23,1,23,5,27,1,27,13,31,2,6,31,35,1,5,35,39,1,39,10,43,2,6,43,47,1,47,5,51,1,51,9,55,2,55,6,59,1,59,10,63,2,63,9,67,1,67,5,71,1,71,5,75,2,75,6,79,1,5,79,83,1,10,83,87,2,13,87,91,1,10,91,95,2,13,95,99,1,99,9,103,1,5,103,107,1,107,10,111,1,111,5,115,1,115,6,119,1,119,10,123,1,123,10,127,2,127,13,131,1,13,131,135,1,135,10,139,2,139,6,143,1,143,9,147,2,147,6,151,1,5,151,155,1,9,155,159,2,159,6,163,1,163,2,167,1,10,167,0,99,2,14,0,0]

    expected = 19690720

    for a in range(0, 99):
        for b in range(0, 99):
            il = copy.deepcopy(input_list)

            try:
                result = intcode(il, a, b)
            except NonHaltException:
                continue

            if result == expected:
                print('Found the solution:', a, b)
                return


if __name__ == '__main__':
    main()