-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
189 lines (167 loc) · 3.85 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_info_from_lines(lines, fold_instr="fold along "):
read_dot = True
dots = set()
folds = []
for l in lines:
l = l.strip()
if l == "":
read_dot = False
elif read_dot:
dots.add(tuple(int(v) for v in l.split(",")))
elif l.startswith(fold_instr):
axis, line = l[len(fold_instr) :].split("=")
folds.append((axis, int(line)))
return dots, folds
def get_info_from_file(file_path=top_dir + "resources/year2021_day13_input.txt"):
with open(file_path) as f:
return get_info_from_lines(f)
def get_paper(dots):
x_vals = {d[0] for d in dots}
y_vals = {d[1] for d in dots}
x_range = list(range(min(x_vals), 1 + max(x_vals)))
y_range = list(range(min(y_vals), 1 + max(y_vals)))
lst = []
for y in y_range:
lst.append("".join("#" if (x, y) in dots else "." for x in x_range))
return "\n".join(lst)
def fold_val(val, line):
assert val != line
return val if val < line else 2 * line - val
def fold_dot(dot, axis, line):
return tuple(fold_val(c, line) if i == axis else c for i, c in enumerate(dot))
def fold_dots(dots, axis, line):
return {fold_dot(d, axis, line) for d in dots}
def apply_folds(dots, folds):
axis_vals = {"x": 0, "y": 1}
for axis, line in folds:
dots = fold_dots(dots, axis_vals[axis], line)
return dots
def run_tests():
info = [
"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",
]
dots, folds = get_info_from_lines(info)
assert dots == {
(9, 10),
(6, 12),
(2, 14),
(9, 0),
(8, 4),
(3, 4),
(10, 4),
(0, 13),
(0, 3),
(8, 10),
(3, 0),
(6, 10),
(10, 12),
(6, 0),
(1, 10),
(4, 11),
(0, 14),
(4, 1),
}
# Step by step
dots = fold_dots(dots, 1, 7)
assert dots == {
(0, 1),
(9, 0),
(6, 2),
(8, 4),
(3, 4),
(4, 3),
(0, 0),
(10, 4),
(0, 3),
(2, 0),
(6, 4),
(1, 4),
(3, 0),
(6, 0),
(4, 1),
(10, 2),
(9, 4),
}
dots = fold_dots(dots, 0, 5)
assert dots == {
(0, 1),
(4, 4),
(2, 4),
(4, 0),
(0, 4),
(3, 4),
(4, 3),
(0, 0),
(0, 3),
(2, 0),
(4, 2),
(1, 4),
(3, 0),
(0, 2),
(1, 0),
(4, 1),
}
# All at once
dots, folds = get_info_from_lines(info)
dots = apply_folds(dots, folds)
assert dots == {
(0, 1),
(4, 4),
(2, 4),
(4, 0),
(0, 4),
(3, 4),
(4, 3),
(0, 0),
(0, 3),
(2, 0),
(4, 2),
(1, 4),
(3, 0),
(0, 2),
(1, 0),
(4, 1),
}
def get_solutions():
dots, folds = get_info_from_file()
print(len(apply_folds(dots, [folds[0]])) == 678)
print(
get_paper(apply_folds(dots, folds))
== """\
####..##..####.#..#.#....#..#.####.####
#....#..#.#....#..#.#....#..#....#.#...
###..#....###..####.#....####...#..###.
#....#....#....#..#.#....#..#..#...#...
#....#..#.#....#..#.#....#..#.#....#...
####..##..#....#..#.####.#..#.####.#..."""
)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)