summaryrefslogtreecommitdiffstats
path: root/solutions/py/d07.py
blob: e12d7c95b885fc8293b997772e716ccabc17f329 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import intcode
import itertools
import queue

def pt1(input):
    program = [int(x) for x in input[0].split(",")]
    highest_signal = 0
    highest_sequence = None
    for phase_seq in list(itertools.permutations(list(range(0,5)))):
        q = queue.Queue(5)
        for phase in phase_seq:
            q.put(phase)
        amps = [intcode.Computer(program) for _ in range(5)]
        for amp in amps:
            amp.input = q.get()
        signal = 0
        for amp in amps:
            while True:
                amp.step()
                if amp.input is None:
                    amp.input = signal
                if amp.output is not None:
                    signal = amp.output
                    amp.output = None
                    break
        if signal > highest_signal:
            highest_signal = signal
            highest_sequence = phase_seq
    return (highest_sequence, highest_signal)

def pt2(input):
    program = [int(x) for x in input[0].split(",")]
    highest_signal = 0
    highest_sequence = None
    for phase_seq in list(itertools.permutations(list(range(5,10)))):
        q = queue.Queue(5)
        for phase in phase_seq:
            q.put(phase)
        amps = [intcode.Computer(program) for _ 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 is 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_sequence, highest_signal)