summaryrefslogtreecommitdiffstats
path: root/solutions/py/07.py
blob: 18c3b89b329eb5797a98b942fc15598376dfebdd (plain) (blame)
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
import intcode
import itertools
import queue

def pt2(program):
    highest_signal = 0
    highest_sequence = None
    for phase_seq in list(itertools.permutations(list(range(5,10)))):
        signal = 0
        q = queue.Queue(5)
        for phase in phase_seq:
            q.put(phase)
        amps = [intcode.Computer(program) for i in range(5)]
        for amp in amps:
            amp.input = q.get()

        signal = 0
        current_amp = 0
        while True:
            amp = amps[current_amp]
            amp.step()
            if amp.input == None:
                if amp.phase_read == False:
                    amp.phase_read = True
                    amp.input = signal
                else:
                    pass
            if amp.output is not None:
                signal = amp.output
                amp.output = None
                current_amp = (current_amp + 1) % 5
                if amps[current_amp].phase_read == True:
                    amps[current_amp].input = signal
                continue
            if amp.memory[amp.pointer] == 99:
                if current_amp == 4:
                    break
                current_amp = (current_amp + 1) % 5
                amps[current_amp].input = signal
                continue
        if signal > highest_signal:
            highest_signal = signal
            highest_sequence = phase_seq
    return highest_signal

if __name__ == "__main__":
    f = open("../input/07", "r")
    program = [int(x) for x in f.readline().split(",")]

    import cProfile
    import timeit

    print(2, pt2(program))
    print(timeit.timeit('pt2(program)', globals=globals(), number=1)*1000, "ms")
    cProfile.run("pt2(program)")