Solve 2019 day 11
This commit is contained in:
93
2019/d11.py
Normal file
93
2019/d11.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
from lib import get_data, str_to_ints, add2
|
||||||
|
from d9 import Amp
|
||||||
|
|
||||||
|
DIRS = [(-1, 0), (0, 1), (1, 0), (0, -1)]
|
||||||
|
BLACK = 0
|
||||||
|
WHITE = 1
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
xs = str_to_ints(data)
|
||||||
|
a = Amp(xs)
|
||||||
|
current_dir_idx = 0
|
||||||
|
pos = (0, 0)
|
||||||
|
white_panels = set()
|
||||||
|
got_painted = set()
|
||||||
|
|
||||||
|
while not a.done:
|
||||||
|
if pos in white_panels:
|
||||||
|
a.feed(WHITE)
|
||||||
|
else:
|
||||||
|
a.feed(BLACK)
|
||||||
|
a.go()
|
||||||
|
a.go()
|
||||||
|
while a.outputs:
|
||||||
|
paint_color = a.pop()
|
||||||
|
if paint_color == BLACK:
|
||||||
|
white_panels.remove(pos)
|
||||||
|
elif paint_color == WHITE:
|
||||||
|
white_panels.add(pos)
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
got_painted.add(pos)
|
||||||
|
turn_dir_code = a.pop()
|
||||||
|
turn_dir = -1 if turn_dir_code == 0 else 1
|
||||||
|
current_dir_idx = (current_dir_idx + turn_dir) % len(DIRS)
|
||||||
|
pos = add2(pos, DIRS[current_dir_idx])
|
||||||
|
print(len(got_painted))
|
||||||
|
|
||||||
|
|
||||||
|
def part_2(data):
|
||||||
|
xs = str_to_ints(data)
|
||||||
|
a = Amp(xs)
|
||||||
|
current_dir_idx = 0
|
||||||
|
pos = (0, 0)
|
||||||
|
white_panels = set([pos])
|
||||||
|
got_painted = set()
|
||||||
|
|
||||||
|
while not a.done:
|
||||||
|
if pos in white_panels:
|
||||||
|
a.feed(WHITE)
|
||||||
|
else:
|
||||||
|
a.feed(BLACK)
|
||||||
|
a.go()
|
||||||
|
a.go()
|
||||||
|
while a.outputs:
|
||||||
|
paint_color = a.pop()
|
||||||
|
if paint_color == BLACK:
|
||||||
|
if pos in white_panels:
|
||||||
|
white_panels.remove(pos)
|
||||||
|
elif paint_color == WHITE:
|
||||||
|
white_panels.add(pos)
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
got_painted.add(pos)
|
||||||
|
turn_dir_code = a.pop()
|
||||||
|
turn_dir = -1 if turn_dir_code == 0 else 1
|
||||||
|
current_dir_idx = (current_dir_idx + turn_dir) % len(DIRS)
|
||||||
|
pos = add2(pos, DIRS[current_dir_idx])
|
||||||
|
|
||||||
|
white_panels = [(p[1], p[0]) for p in white_panels]
|
||||||
|
xs = [p[0] for p in white_panels]
|
||||||
|
ys = [p[1] for p in white_panels]
|
||||||
|
white_panels_set = set(white_panels)
|
||||||
|
x_min, x_max = min(xs), max(xs)
|
||||||
|
y_min, y_max = min(ys), max(ys)
|
||||||
|
for y in range(y_min, y_max + 1):
|
||||||
|
s = ""
|
||||||
|
for x in range(x_min, x_max + 1):
|
||||||
|
if (x, y) in white_panels_set:
|
||||||
|
s += "X"
|
||||||
|
else:
|
||||||
|
s += " "
|
||||||
|
print(s)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
data = get_data(__file__)
|
||||||
|
part_1(data)
|
||||||
|
part_2(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -120,7 +120,8 @@ Solutions and utility script for Advent of Code challenges in Python.
|
|||||||
- Day 8: 08:55 (54th)
|
- Day 8: 08:55 (54th)
|
||||||
- Day 9: 115:00 (Try to read next time.)
|
- Day 9: 115:00 (Try to read next time.)
|
||||||
- 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:
|
- Day 11: 21:04 (Too slow, but fun.)
|
||||||
|
- Day 12:
|
||||||
|
|
||||||
## AoC 2022
|
## AoC 2022
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user