56 lines
1.1 KiB
Python
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()
|