Day 13 (Generator)

This commit is contained in:
Sebastian Seedorf
2021-12-13 14:24:04 +01:00
parent 964296b439
commit bac46a6cf9
3 changed files with 41 additions and 2 deletions

41
day13/generator.py Normal file
View File

@@ -0,0 +1,41 @@
from itertools import repeat
from random import random, randrange
target = """
# # ####### # # ####### ###### ##### ####### # # # # # ##### ### # # ####### # # ####### # # # # ######
## # # # # # # # # # # # ## # ## # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # ##### # # ##### ###### # #### # # # # # # # # # # # #### # # # ##### # # # # # # # ######
# # # # # # # # # # # # # # # # # # # ####### # # # # # # # # # # # # # #
# ## # # # # # # # # # # # ## # ## # # # # # # # # # # # # # # # #
# # ####### # ####### # # ##### ####### # # # # # # ##### ### # ####### # ####### ##### ##### #
"""
points = {(x, y) for y, line in enumerate(target.strip().split('\n')) for x, c in enumerate(line) if c == "#"}
img_x, img_y = max_x, max_y = tuple(map(max, zip(*points)))
instructions = []
for _ in range(20):
is_x, fold = (True, randrange(max_x+1, max_x*2))\
if random() < 0.5\
else (False, randrange(max_y+1, max_y*2))
instructions.append((is_x, fold))
next_max = fold*2
if is_x:
points = set(v for (x, y), rand in zip(points, iter(random, 2)) for v in [
(x, y) if rand <= 0.66 or x <= max_x-next_max+fold else None,
(2*fold - x, y) if rand > 0.33 and x > max_x-next_max+fold else None,
] if v is not None)
max_x = next_max
else:
points = set(v for (x, y), rand in zip(points, iter(random, 2)) for v in [
(x, y) if rand <= 0.66 or y <= max_y-next_max+fold else None,
(x, 2*fold - y) if rand > 0.33 and y > max_y-next_max+fold else None,
] if v is not None)
max_y = next_max
for point in points:
print(','.join(map(str, point)))
print()
for instruction in instructions[::-1]:
print("fold along " + ("x" if instruction[0] else "y") + "=" + str(instruction[1]))

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
lines = (x.strip() for x in open("input.txt")) lines = (x.strip() for x in open("input.txt"))
points = set() points = set()

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
lines = (x.strip() for x in open("input.txt")) lines = (x.strip() for x in open("input.txt"))
points = set() points = set()