Solve 2019 day 13
This commit is contained in:
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.inputs = []
|
||||||
self.outputs = []
|
self.outputs = []
|
||||||
self.done = False
|
self.done = False
|
||||||
|
self.input_required = False
|
||||||
self.rel_base = 0
|
self.rel_base = 0
|
||||||
|
|
||||||
def feed(self, input):
|
def feed(self, input):
|
||||||
@@ -72,7 +73,10 @@ class Amp:
|
|||||||
i += 4
|
i += 4
|
||||||
case 3:
|
case 3:
|
||||||
# read input
|
# 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)
|
addr = self.get_addr(i + 1, mode_p1)
|
||||||
xs[addr] = self.inputs[0]
|
xs[addr] = self.inputs[0]
|
||||||
self.inputs = self.inputs[1:]
|
self.inputs = self.inputs[1:]
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ Solutions and utility script for Advent of Code challenges in Python.
|
|||||||
- Day 10: 76:00 (This wasn't easy for me. Fun, though.)
|
- Day 10: 76:00 (This wasn't easy for me. Fun, though.)
|
||||||
- Day 11: 21:04 (Too slow, but fun.)
|
- Day 11: 21:04 (Too slow, but fun.)
|
||||||
- Day 12:
|
- Day 12:
|
||||||
|
- Day 13: >120:00 (Just struggling so much for some reason.)
|
||||||
|
|
||||||
## AoC 2022
|
## AoC 2022
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user