-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.py
87 lines (70 loc) · 1.5 KB
/
13.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
INPUT_FILE = "input_13"
SAMPLE = """6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5"""
def fold(dots, line):
which, n = line.split("=")
n = int(n)
new_dots = set()
if which == "y":
for xx, yy in dots:
if yy < n:
new_dots.add((xx, yy))
else:
new_dots.add((xx, n - (yy - n)))
elif which == "x":
for xx, yy in dots:
if xx < n:
new_dots.add((xx, yy))
else:
new_dots.add((n - (xx - n), yy))
return new_dots
def print_dots(dots):
xmax = max(xx for xx, yy in dots)
ymax = max(yy for xx, yy in dots)
for yy in range(ymax + 1):
for xx in range(xmax + 1):
if (xx, yy) in dots:
print("#", end="")
else:
print(".", end="")
print()
if __name__ == "__main__":
dots_done = False
dots = set()
folds = []
with open(INPUT_FILE) as f:
for line in f:
# for line in SAMPLE.splitlines():
line = line.strip()
if line == "":
dots_done = True
continue
if dots_done:
folds.append(line.split()[2])
else:
dots.add(tuple(map(int, line.split(","))))
# Part 1
print(len(fold(dots, folds[0])))
# Part 2
out = dots.copy()
for f in folds:
out = fold(out, f)
print_dots(out)