-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
49 lines (42 loc) · 1.4 KB
/
day13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
from collections import defaultdict
def fold(paper, folds):
for (fx, fy) in folds:
for (hx, hy) in paper.copy():
if fy != 0 and hy > fy:
del paper[(hx, hy)]
paper[(hx, fy - (hy - fy))] = 1
elif fx != 0 and hx > fx:
del paper[(hx, hy)]
paper[(fx - (hx - fx), hy)] = 1
return sum(paper.values())
def printPaper(paper):
maxX = 1 + max([ hole[0] for hole in paper ])
maxY = 1 + max([ hole[1] for hole in paper ])
for j in range(maxY):
for i in range(maxX):
print("#" if paper[(i,j)] == 1 else " ", end="")
print()
print()
def main(args = ()):
fileName = "day13.txt" if len(args) < 1 else args[0]
paper = defaultdict(int)
folds = []
with open(fileName) as lines:
for line in lines:
line = line.strip()
if "," in line:
x, y = [ int(i) for i in line.split(",") ]
paper[(x,y)] = 1
elif "x=" in line:
folds.append((int(line.split("=")[1]), 0))
elif "y=" in line:
folds.append((0, int(line.split("=")[1])))
part1 = fold(paper, [ folds.pop(0) ])
print("Part 1:", part1)
# part 2: continue folding
fold(paper, folds)
print("Part 2:")
printPaper(paper)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))