aoc/2021/13/13.py
2021-12-18 10:40:06 -06:00

56 lines
1.1 KiB
Python

paper = {}
instructions = []
with open("input.txt") as f:
points, folds = f.read().split('\n\n')
for point in points.split('\n'):
x, y = point.split(',')
paper[(int(x),int(y))] = True
for fold in folds.split('\n'):
axis = fold[11:]
axis, value = axis.split('=')
instructions.append((axis, int(value)))
def fold(paper, instruction):
axis, value = instruction
if axis == 'x':
return fold_x(paper, value)
elif axis == 'y':
return fold_y(paper, value)
def fold_x(paper, value):
folded_paper = {}
for point in paper:
x, y = point
if x > value:
x = value - (x - value)
folded_paper[(x,y)] = True
return folded_paper
def fold_y(paper, value):
folded_paper = {}
for point in paper:
x, y = point
if y > value:
y = value - (y - value)
folded_paper[(x,y)] = True
return folded_paper
part1 = fold(paper, instructions[0])
print(len(part1))
for instruction in instructions:
paper = fold(paper, instruction)
xs = []
ys = []
for point in paper:
x, y = point
xs.append(x)
ys.append(y)
width = max(xs)
height = max(ys)
from matplotlib import pyplot as plot
plot.scatter(xs,ys)
plot.axis('equal')
plot.show()