day13 (can p2 be solved without simulating the full game?)

This commit is contained in:
Stefan Harmuth 2021-12-02 05:45:38 +01:00
parent f4a79d5e07
commit e3694fd28f
4 changed files with 56 additions and 3 deletions

View File

@ -32,9 +32,9 @@ def moveMoons(moons: List[Moon]):
if i == t:
continue
moon.vel_x += compare(moon.x, moons[t].x)
moon.vel_y += compare(moon.y, moons[t].y)
moon.vel_z += compare(moon.z, moons[t].z)
moon.vel_x += compare(moons[t].x, moon.x)
moon.vel_y += compare(moons[t].y, moon.y)
moon.vel_z += compare(moons[t].z, moon.z)
for moon in moons:
moon.x += moon.vel_x

49
day13.py Normal file
View File

@ -0,0 +1,49 @@
import time
from aoc import AOCDay
from intcode import IntCode
from 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

1
inputs/input13 Normal file

File diff suppressed because one or more lines are too long

View File

@ -58,6 +58,9 @@ class IntCode(threading.Thread):
def addInput(self, value: int):
self.input_queue.put(value)
def hasOutput(self):
return self.output_queue.qsize() > 0
def getOutput(self, count: int = 1, block: bool = True, timeout: int = None) -> Union[int, List[int]]:
if count == 1:
res = self.output_queue.get(block=block, timeout=timeout)