-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_13.py
102 lines (86 loc) · 2.47 KB
/
day_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""AOC Day 13"""
import pathlib
import time
TEST_INPUT = """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 read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def extract(input_data: str) -> tuple:
"""take input data and return the appropriate data structure"""
sheet = set()
folds = list()
s_instr, f_instr = input_data.split('\n\n')
for line in s_instr.split('\n'):
sheet.add(tuple(map(int, line.split(','))))
for line in f_instr.split('\n'):
equal_pos = line.index('=')
folds.append((line[equal_pos-1], int(line[equal_pos+1:])))
return (sheet, folds)
def fold(sheet: set, direction: str, axis: int):
folded = set()
for x, y in sheet:
if direction == 'x' and x > axis:
x = 2 * axis - x
elif direction == 'y' and y > axis:
y = 2 * axis - y
folded.add((x, y))
return folded
def part1(entries: tuple) -> int:
"""part1 solver take the entries and return the part1 solution"""
direction, axis = entries[1][0]
sheet = fold(entries[0], direction, axis)
return len(sheet)
def part2(entries: tuple) -> str:
"""part2 solver take the entries and return the part2 solution"""
sheet = entries[0]
fold_instructions = entries[1]
for direction, axis in fold_instructions:
sheet = fold(sheet, direction, axis)
max_x = max(p[0] for p in sheet)
max_y = max(p[1] for p in sheet)
out = ''
for y in range(max_y + 1):
for x in range(max_x + 1):
out += '#' if (x, y) in sheet else ' '
out += '\n'
return out
def test_input_day_13():
"""pytest testing function"""
entries = extract(TEST_INPUT)
assert part1(entries) == 17
def test_bench_day_13(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
entries = extract(input_data)
print("Part 1: %d" % part1(entries))
print("Part 2:\n%s" % part2(entries))
end_time = time.time()
print("Execution time: %f" % (end_time-start_time))
if __name__ == "__main__":
main()