Files
advent-of-code-2021/day-13/day-13.py
Pascal Lais 03633c432f
All checks were successful
continuous-integration/drone/push Build is passing
Fix day 13 part 2 output
2021-12-13 12:45:58 +01:00

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 + 1):
for x in range(max_x + 1):
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)