mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2019 day 7
This commit is contained in:
69
2019/aoc2019/day07.py
Normal file
69
2019/aoc2019/day07.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import itertools
|
||||||
|
from typing import List, TextIO, Tuple
|
||||||
|
|
||||||
|
from aoc2019.intcode import read_program, Computer
|
||||||
|
|
||||||
|
|
||||||
|
def amplify(phases: Tuple[int], program: List[int]) -> int:
|
||||||
|
amps = []
|
||||||
|
|
||||||
|
for i, phase in enumerate(phases):
|
||||||
|
amp = Computer(program.copy())
|
||||||
|
|
||||||
|
if i > 0:
|
||||||
|
amp.input = amps[i - 1].output
|
||||||
|
|
||||||
|
amp.input.append(phase)
|
||||||
|
|
||||||
|
amps.append(amp)
|
||||||
|
|
||||||
|
amps[0].input.append(0)
|
||||||
|
|
||||||
|
for amp in amps:
|
||||||
|
amp.run()
|
||||||
|
|
||||||
|
return amps[-1].output.pop()
|
||||||
|
|
||||||
|
|
||||||
|
def reamplify(phases: Tuple[int], program: List[int]) -> int:
|
||||||
|
amps = []
|
||||||
|
|
||||||
|
for i, _ in enumerate(phases):
|
||||||
|
amp = Computer(program.copy())
|
||||||
|
|
||||||
|
if i > 0:
|
||||||
|
amp.input = amps[i - 1].output
|
||||||
|
|
||||||
|
amps.append(amp)
|
||||||
|
|
||||||
|
amps[0].input = amps[-1].output
|
||||||
|
|
||||||
|
for amp, phase in zip(amps, phases):
|
||||||
|
amp.input.append(phase)
|
||||||
|
|
||||||
|
amps[0].input.append(0)
|
||||||
|
|
||||||
|
changes = True
|
||||||
|
|
||||||
|
while changes:
|
||||||
|
changes = False
|
||||||
|
for amp in amps:
|
||||||
|
try:
|
||||||
|
amp.run()
|
||||||
|
except IndexError:
|
||||||
|
# Waiting for input
|
||||||
|
changes = True
|
||||||
|
|
||||||
|
return amps[-1].output.pop()
|
||||||
|
|
||||||
|
|
||||||
|
def part1(data: TextIO) -> int:
|
||||||
|
program = read_program(data)
|
||||||
|
|
||||||
|
return max(amplify(phase, program) for phase in itertools.permutations(range(0, 5)))
|
||||||
|
|
||||||
|
|
||||||
|
def part2(data: TextIO) -> int:
|
||||||
|
program = read_program(data)
|
||||||
|
|
||||||
|
return max(reamplify(phase, program) for phase in itertools.permutations(range(5, 10)))
|
||||||
27
2019/tests/test_day07.py
Normal file
27
2019/tests/test_day07.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
from io import StringIO
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from aoc2019.day07 import part1, part2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('maximum,program', [
|
||||||
|
(43210, "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0"),
|
||||||
|
(54321, "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0"),
|
||||||
|
(65210, "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0"),
|
||||||
|
])
|
||||||
|
def test_sample_part1(maximum: int, program: str) -> None:
|
||||||
|
data = StringIO(program)
|
||||||
|
|
||||||
|
assert part1(data) == maximum
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('maximum,program', [
|
||||||
|
(139629729, "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5"),
|
||||||
|
(18216, "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,"
|
||||||
|
"53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10"),
|
||||||
|
])
|
||||||
|
def test_sample_part2(maximum: int, program: str) -> None:
|
||||||
|
data = StringIO(program)
|
||||||
|
|
||||||
|
assert part2(data) == maximum
|
||||||
Reference in New Issue
Block a user