-
Notifications
You must be signed in to change notification settings - Fork 0
/
a13.py
102 lines (81 loc) · 1.86 KB
/
a13.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
example = """
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
""".strip()
f = open("input/13.input")
text = f.read().strip()
def parse(t):
dots_text, folds_text = t.split("\n\n")
dots = set()
for line in dots_text.split("\n"):
x, y = [int(a) for a in line.split(",")]
dots.add((x, y))
folds = []
for line in folds_text.split("\n"):
equation = line.split()[2]
axis, value = equation.split("=")
folds.append((axis, int(value)))
return dots, folds
def fold_paper(paper, at):
result = set()
if at[0] == "x":
for coord in paper:
if coord[0] < at[1]:
result.add(coord)
else:
new_coord = (at[1] * 2 - coord[0], coord[1])
result.add(new_coord)
else:
for coord in paper:
if coord[1] < at[1]:
result.add(coord)
else:
new_coord = (coord[0], at[1] * 2 - coord[1])
result.add(new_coord)
return result
def part1(text):
paper, folds = parse(text)
paper = fold_paper(paper, folds[0])
return len(paper)
def display_paper(paper):
total_y = max([coord[1] for coord in paper])
total_x = max([coord[0] for coord in paper])
res = "\n"
for y in range(total_y + 1):
for x in range(total_x + 1):
if (x, y) in paper:
res += '█'
else:
res += " "
res += "\n"
return res
def part2(text):
paper, folds = parse(text)
for fold_equation in folds:
paper = fold_paper(paper, fold_equation)
return display_paper(paper)
def main(text):
print("Part1:", part1(text))
print("Part2:", part2(text))
print("==== EXAMPLES ====")
main(example)
print("==== SOLUTION RESULT ====")
main(text)