Skip to content

Commit 9971b04

Browse files
committed
Day 13 in Python
1 parent e38b8f1 commit 9971b04

File tree

3 files changed

+1033
-0
lines changed

3 files changed

+1033
-0
lines changed

day13.py

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

0 commit comments

Comments
 (0)