Solve 2019 day 13
This commit is contained in:
parent
fb1e2183cc
commit
6efa70ea51
50
2019/d13.py
Normal file
50
2019/d13.py
Normal file
@ -0,0 +1,50 @@
|
||||
from lib import get_data, str_to_ints
|
||||
from d9 import Amp
|
||||
|
||||
|
||||
def part_1(data):
|
||||
xs = str_to_ints(data)
|
||||
a = Amp(xs)
|
||||
while not a.done:
|
||||
a.go()
|
||||
|
||||
r = sum([a.outputs[i:i+3][2] == 2 for i in range(0, len(a.outputs), 3)])
|
||||
print(r)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
xs = str_to_ints(data)
|
||||
xs[0] = 2 # play for free
|
||||
a = Amp(xs)
|
||||
ball_x = 0
|
||||
paddle_x = 0
|
||||
score = 0
|
||||
while not a.done:
|
||||
a.go()
|
||||
if len(a.outputs) == 3:
|
||||
x, y, tile_id = a.pop(), a.pop(), a.pop()
|
||||
if x == -1 and y == 0:
|
||||
score = tile_id
|
||||
elif tile_id == 4:
|
||||
ball_x = x
|
||||
elif tile_id == 3:
|
||||
paddle_x = x
|
||||
if a.input_required:
|
||||
a.input_required = False
|
||||
x = 0
|
||||
if ball_x < paddle_x:
|
||||
x = -1
|
||||
elif ball_x > paddle_x:
|
||||
x = 1
|
||||
a.feed(x)
|
||||
print(score)
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -9,6 +9,7 @@ class Amp:
|
||||
self.inputs = []
|
||||
self.outputs = []
|
||||
self.done = False
|
||||
self.input_required = False
|
||||
self.rel_base = 0
|
||||
|
||||
def feed(self, input):
|
||||
@ -72,7 +73,10 @@ class Amp:
|
||||
i += 4
|
||||
case 3:
|
||||
# read input
|
||||
assert len(self.inputs) > 0
|
||||
if len(self.inputs) == 0:
|
||||
self.i = i
|
||||
self.input_required = True
|
||||
return
|
||||
addr = self.get_addr(i + 1, mode_p1)
|
||||
xs[addr] = self.inputs[0]
|
||||
self.inputs = self.inputs[1:]
|
||||
|
Loading…
Reference in New Issue
Block a user