51 lines
1007 B
Python
51 lines
1007 B
Python
![]() |
#!/usr/bin/env python
|
||
|
|
||
|
import re
|
||
|
import copy
|
||
|
|
||
|
dots = dict()
|
||
|
folds = []
|
||
|
|
||
|
def do_fold_x(dots, line):
|
||
|
copy_dots = copy.copy(dots)
|
||
|
for dot in dots:
|
||
|
x = dot[0]
|
||
|
if x <= line:
|
||
|
continue
|
||
|
x1 = line - (x - line)
|
||
|
copy_dots[(x1, dot[1])] = True
|
||
|
del copy_dots[dot]
|
||
|
return copy_dots
|
||
|
|
||
|
def do_fold_y(dots, line):
|
||
|
copy_dots = copy.copy(dots)
|
||
|
for dot in dots:
|
||
|
y = dot[1]
|
||
|
if y <= line:
|
||
|
continue
|
||
|
y1 = line - (y - line)
|
||
|
copy_dots[(dot[0], y1)] = True
|
||
|
del copy_dots[dot]
|
||
|
return copy_dots
|
||
|
|
||
|
RE_FOLD = re.compile("fold along ([xy])=(\d+)$")
|
||
|
with open("input01.txt","r") as f:
|
||
|
for line in f:
|
||
|
line = line.strip()
|
||
|
if len(line) == 0:
|
||
|
break
|
||
|
tmp = line.split(",",2)
|
||
|
dots[(int(tmp[0]), int(tmp[1]))] = True
|
||
|
for line in f:
|
||
|
line = line.strip()
|
||
|
if len(line) == 0:
|
||
|
break
|
||
|
m = RE_FOLD.match(line)
|
||
|
folds.append((m.group(1), int(m.group(2))))
|
||
|
|
||
|
if folds[0][0] == 'x':
|
||
|
dots = do_fold_x(dots, folds[0][1])
|
||
|
if folds[0][0] == 'y':
|
||
|
dots = do_fold_y(dots, folds[0][1])
|
||
|
|
||
|
print(len(dots.values()))
|