50 lines
1.2 KiB
Python
50 lines
1.2 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):
|
|
test_solutions_p1 = []
|
|
test_solutions_p2 = []
|
|
|
|
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()
|
|
|
|
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
|