Tidy up day 13 solution
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,17 +2,17 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def fold(i, dots):
|
def fold(axis, fold_at, dots):
|
||||||
new_dots = set()
|
new_dots = set()
|
||||||
for d in dots:
|
for (x, y) in dots:
|
||||||
if i[0] == 'x':
|
if axis == 'x':
|
||||||
n = (i[1] - abs(d[0] - i[1]), d[1])
|
new_x = fold_at - abs(x - fold_at)
|
||||||
if d[0] != i[1]:
|
if new_x != fold_at:
|
||||||
new_dots.add(n)
|
new_dots.add((new_x, y))
|
||||||
elif i[0] == 'y':
|
elif axis == 'y':
|
||||||
n = (d[0], i[1] - abs(d[1] - i[1]))
|
new_y = fold_at - abs(y - fold_at)
|
||||||
if d[1] != i[1]:
|
if new_y != fold_at:
|
||||||
new_dots.add(n)
|
new_dots.add((x, new_y))
|
||||||
return new_dots
|
return new_dots
|
||||||
|
|
||||||
|
|
||||||
@@ -27,7 +27,8 @@ def part_1(input):
|
|||||||
elif 'fold along' in line:
|
elif 'fold along' in line:
|
||||||
d, c = line.strip().split()[-1].split('=')
|
d, c = line.strip().split()[-1].split('=')
|
||||||
instructions.append((d, int(c)))
|
instructions.append((d, int(c)))
|
||||||
dots = fold(instructions.pop(0), dots)
|
axis, fold_at = instructions.pop(0)
|
||||||
|
dots = fold(axis, fold_at, dots)
|
||||||
result = len(dots)
|
result = len(dots)
|
||||||
print("Part 1 result:", result)
|
print("Part 1 result:", result)
|
||||||
|
|
||||||
@@ -43,14 +44,11 @@ def part_2(input):
|
|||||||
elif 'fold along' in line:
|
elif 'fold along' in line:
|
||||||
d, c = line.strip().split()[-1].split('=')
|
d, c = line.strip().split()[-1].split('=')
|
||||||
instructions.append((d, int(c)))
|
instructions.append((d, int(c)))
|
||||||
|
|
||||||
while len(instructions):
|
while len(instructions):
|
||||||
dots = fold(instructions.pop(0), dots)
|
axis, fold_at = instructions.pop(0)
|
||||||
max_x = 0
|
dots = fold(axis, fold_at, dots)
|
||||||
max_y = 0
|
max_x = max([x for x, _ in dots])
|
||||||
for d in dots:
|
max_y = max([y for _, y in dots])
|
||||||
max_x = max(max_x, d[0] + 1)
|
|
||||||
max_y = max(max_y, d[1] + 1)
|
|
||||||
print("Part 2 result:")
|
print("Part 2 result:")
|
||||||
for y in range(max_y):
|
for y in range(max_y):
|
||||||
for x in range(max_x):
|
for x in range(max_x):
|
||||||
|
|||||||
Reference in New Issue
Block a user