Files
advent-of-code-2021/day-13/day-13.py
Pascal Lais 7928680a02
All checks were successful
continuous-integration/drone/push Build is passing
Fix day 13 output
2021-12-13 06:40:40 +01:00

72 lines
1.8 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
def fold(i, dots):
new_dots = []
if i[0] == 'x':
for d in dots:
n = (i[1] - abs(d[0] - i[1]), d[1])
if d[0] != i[1] and n not in new_dots:
new_dots.append(n)
elif i[0] == 'y':
for d in dots:
n = (d[0], i[1] - abs(d[1] - i[1]))
if d[1] != i[1] and n not in new_dots:
new_dots.append(n)
return new_dots
def part_1(input):
result = 0
dots = []
instructions = []
for line in input:
if ',' in line:
x, y = line.strip().split(',')
dots.append((int(x), int(y)))
elif 'fold along' in line:
d, c = line.strip().split()[-1].split('=')
instructions.append([d, int(c)])
dots = fold(instructions.pop(0), dots)
result = len(dots)
print("Part 1 result:", result)
def part_2(input):
result = 0
dots = []
instructions = []
for line in input:
if ',' in line:
x, y = line.strip().split(',')
dots.append((int(x), int(y)))
elif 'fold along' in line:
d, c = line.strip().split()[-1].split('=')
instructions.append([d, int(c)])
while len(instructions):
dots = fold(instructions.pop(0), dots)
max_x = 0
max_y = 0
for d in dots:
max_x = max(max_x, d[0] + 1)
max_y = max(max_y, d[1] + 1)
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)