62 lines
1.4 KiB
Python
62 lines
1.4 KiB
Python
|
|
import time
|
|
from intcode import IntCode
|
|
from tools.aoc import AOCDay
|
|
from tools.tools import compare
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(236, "input13")
|
|
],
|
|
[
|
|
(11040, "input13")
|
|
]
|
|
]
|
|
|
|
def part1(self) -> Any:
|
|
comp = IntCode(self.getInputAsArraySplit(",", int))
|
|
comp.run()
|
|
|
|
blocks = 0
|
|
while comp.hasOutput():
|
|
blocks += comp.getOutput(3)[2] == 2
|
|
|
|
return blocks
|
|
|
|
def part2(self) -> Any:
|
|
comp = IntCode(self.getInputAsArraySplit(",", int))
|
|
comp.memory[0] = 2
|
|
comp.start()
|
|
time.sleep(0.010) # let the comp build it's output
|
|
|
|
score = 0
|
|
while comp.isRunning():
|
|
paddle = []
|
|
ball_x = 0
|
|
paddle_x = 0
|
|
time.sleep(0.005) # let the comp build it's output
|
|
while comp.hasOutput():
|
|
x = comp.getOutput()
|
|
tile = comp.getOutput(2)[1]
|
|
|
|
if x == -1:
|
|
score = tile
|
|
elif tile == 4:
|
|
ball_x = x
|
|
elif tile == 3:
|
|
paddle.append(x)
|
|
|
|
if len(paddle) > 0:
|
|
paddle_x = sum(paddle) // len(paddle)
|
|
comp.addInput(compare(ball_x, paddle_x))
|
|
|
|
return score
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2019, 13)
|
|
day.run(verbose=True)
|