Solve easy d19 2019 because I need stars
This commit is contained in:
parent
e9f5542be8
commit
dd901c9f88
52
2019/d19.py
Normal file
52
2019/d19.py
Normal file
@ -0,0 +1,52 @@
|
||||
from lib import get_data, str_to_ints
|
||||
from d9 import Amp
|
||||
|
||||
|
||||
def isin(xs, x, y):
|
||||
a = Amp(xs, 100)
|
||||
a.feed(x)
|
||||
a.feed(y)
|
||||
a.go()
|
||||
return a.pop()
|
||||
|
||||
|
||||
def part_1(data):
|
||||
xs = str_to_ints(data)
|
||||
r = 0
|
||||
for y in range(50):
|
||||
for x in range(50):
|
||||
o = isin(xs, x, y)
|
||||
if o == 1:
|
||||
print("#", end="")
|
||||
else:
|
||||
print(" ", end="")
|
||||
r += o
|
||||
print()
|
||||
print(r)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
xs = str_to_ints(data)
|
||||
off = 99
|
||||
x, y = 3, 4
|
||||
while True:
|
||||
y += 1
|
||||
while True:
|
||||
o = isin(xs, x, y)
|
||||
if o == 1:
|
||||
break
|
||||
x += 1
|
||||
|
||||
if isin(xs, x, y - off) and isin(xs, x + off, y - off) and isin(xs, x + off, y):
|
||||
print(x * 10_000 + y - off)
|
||||
break
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
20
2019/d9.py
20
2019/d9.py
@ -2,9 +2,9 @@ from lib import get_data, str_to_ints
|
||||
|
||||
|
||||
class Amp:
|
||||
def __init__(self, xs):
|
||||
def __init__(self, xs, buffer_extra=10000):
|
||||
self.xs = list(xs)
|
||||
self.xs += [0 for _ in range(10000)]
|
||||
self.xs += [0 for _ in range(buffer_extra)]
|
||||
self.i = 0
|
||||
self.inputs = []
|
||||
self.outputs = []
|
||||
@ -41,12 +41,6 @@ class Amp:
|
||||
else:
|
||||
assert False
|
||||
|
||||
def go_all(self):
|
||||
while not self.done:
|
||||
self.go()
|
||||
while self.outputs:
|
||||
print(self.pop(), end=",")
|
||||
|
||||
def go(self):
|
||||
xs = self.xs
|
||||
i = self.i
|
||||
@ -138,11 +132,17 @@ def part_1(data):
|
||||
|
||||
a = Amp(xs_orig)
|
||||
a.feed(1)
|
||||
a.go_all()
|
||||
while not a.done:
|
||||
a.go()
|
||||
a.go()
|
||||
print(a.pop())
|
||||
|
||||
a = Amp(xs_orig)
|
||||
a.feed(2)
|
||||
a.go_all()
|
||||
while not a.done:
|
||||
a.go()
|
||||
a.go()
|
||||
print(a.pop())
|
||||
|
||||
|
||||
def main():
|
||||
|
Loading…
Reference in New Issue
Block a user