-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-13.py
118 lines (88 loc) · 2.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# --- Day 13: Transparent Origami ---
# https://adventofcode.com/2021/day/13
import re
def parse(input_data):
points, instructions = input_data.split("\n\n")
points = [list(map(int, point.split(","))) for point in points.splitlines()]
instructions = [
instruction.split()[-1].split("=") for instruction in instructions.splitlines()
]
instructions = [(direction, int(n)) for direction, n in instructions]
return points, instructions
def fold(point, instruction):
if instruction[0] == "y":
axis_num = 1
else:
axis_num = 0
if point[axis_num] > instruction[1]:
overage = point[axis_num] - instruction[1]
point[axis_num] = instruction[1] - overage
return point
def fold_points(points, instruction):
new_points = []
for point in points:
new_point = fold(point, instruction)
if new_point not in new_points:
new_points.append(new_point)
return new_points
def solve1(input_data):
points, instructions = parse(input_data)
new_points = fold_points(points, instructions[0])
return len(new_points)
def fold_part_2(input_data):
points, instructions = parse(input_data)
new_points = points
for instruction in instructions:
new_points = fold_points(new_points, instruction)
return new_points
def plot_points(points):
xs, ys = zip(*points)
xmax = max(xs)
ymax = max(ys)
for y in range(ymax + 1):
for x in range(xmax + 1):
if [x, y] in points:
print("#", end="")
else:
print(" ", end="")
print("\n", end="")
def solve2(input_data):
points = fold_part_2(input_data)
return plot_points(points)
if __name__ == "__main__":
from aocd.models import Puzzle
test_data = """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"""
assert solve1(test_data) == 17
puzzle = Puzzle(2021, 13)
answer_1 = solve1(puzzle.input_data)
print(answer_1)
puzzle.answer_a = answer_1
solve2(puzzle.input_data)
puzzle.answer_b = "BLKJRBAG"
"""
### # # # ## ### ### ## ##
# # # # # # # # # # # # # #
### # ## # # # ### # # #
# # # # # # ### # # #### # ##
# # # # # # # # # # # # # # #
### #### # # ## # # ### # # ###
"""