All checks were successful
continuous-integration/drone/push Build is passing
68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
|
|
|
|
def fold(axis, fold_at, dots):
|
|
new_dots = set()
|
|
for (x, y) in dots:
|
|
if axis == 'x':
|
|
new_x = fold_at - abs(x - fold_at)
|
|
if new_x != fold_at:
|
|
new_dots.add((new_x, y))
|
|
elif axis == 'y':
|
|
new_y = fold_at - abs(y - fold_at)
|
|
if new_y != fold_at:
|
|
new_dots.add((x, new_y))
|
|
return new_dots
|
|
|
|
|
|
def part_1(input):
|
|
result = 0
|
|
dots = set()
|
|
instructions = []
|
|
for line in input:
|
|
if ',' in line:
|
|
x, y = line.strip().split(',')
|
|
dots.add((int(x), int(y)))
|
|
elif 'fold along' in line:
|
|
d, c = line.strip().split()[-1].split('=')
|
|
instructions.append((d, int(c)))
|
|
axis, fold_at = instructions.pop(0)
|
|
dots = fold(axis, fold_at, dots)
|
|
result = len(dots)
|
|
print("Part 1 result:", result)
|
|
|
|
|
|
def part_2(input):
|
|
result = 0
|
|
dots = set()
|
|
instructions = []
|
|
for line in input:
|
|
if ',' in line:
|
|
x, y = line.strip().split(',')
|
|
dots.add((int(x), int(y)))
|
|
elif 'fold along' in line:
|
|
d, c = line.strip().split()[-1].split('=')
|
|
instructions.append((d, int(c)))
|
|
while len(instructions):
|
|
axis, fold_at = instructions.pop(0)
|
|
dots = fold(axis, fold_at, dots)
|
|
max_x = max([x for x, _ in dots])
|
|
max_y = max([y for _, y in dots])
|
|
print("Part 2 result:")
|
|
for y in range(max_y):
|
|
for x in range(max_x):
|
|
if (x, y) in dots:
|
|
print('█', end='')
|
|
else:
|
|
print('░', end='')
|
|
print()
|
|
|
|
|
|
input = list()
|
|
p = Path(__file__).with_name('input.txt')
|
|
with open(p) as f:
|
|
input = f.readlines()
|
|
part_1(input)
|
|
part_2(input)
|