Solve 2021 day 12-14
This commit is contained in:
51
2021/d13.py
Normal file
51
2021/d13.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from lib import get_data
|
||||
from lib import ints
|
||||
|
||||
data = get_data(__file__).strip()
|
||||
|
||||
|
||||
def print_from_xy(xs):
|
||||
x_min = min(v[0] for v in xs)
|
||||
x_max = max(v[0] for v in xs)
|
||||
y_min = min(v[1] for v in xs)
|
||||
y_max = max(v[1] for v in xs)
|
||||
for y in range(y_min, y_max + 1):
|
||||
row = ""
|
||||
for x in range(x_min, x_max + 1):
|
||||
if (x, y) in xs:
|
||||
row += "#"
|
||||
else:
|
||||
row += " "
|
||||
print(row)
|
||||
|
||||
|
||||
p1, p2 = data.split("\n\n")
|
||||
xs = set([tuple(ints(line)) for line in p1.splitlines()])
|
||||
|
||||
folds = []
|
||||
for line in p2.splitlines():
|
||||
d = "x" if "x=" in line else "y"
|
||||
(n,) = ints(line)
|
||||
folds.append((d, n))
|
||||
|
||||
|
||||
first = None
|
||||
for d, n in folds:
|
||||
if d == "x":
|
||||
to_move = [(x, y) for (x, y) in xs if x > n]
|
||||
for x, y in to_move:
|
||||
xs.remove((x, y))
|
||||
new_x = n - (x - n)
|
||||
xs.add((new_x, y))
|
||||
elif d == "y":
|
||||
to_move = [(x, y) for (x, y) in xs if y > n]
|
||||
for x, y in to_move:
|
||||
xs.remove((x, y))
|
||||
new_y = n - (y - n)
|
||||
xs.add((x, new_y))
|
||||
else:
|
||||
assert False
|
||||
first = first or len(xs)
|
||||
|
||||
print(first)
|
||||
print_from_xy(xs)
|
||||
Reference in New Issue
Block a user