Skip to content

Commit 9f9a486

Browse files
committed
day13 paper folding
1 parent 3fd6b3c commit 9f9a486

File tree

2 files changed

+828
-0
lines changed

2 files changed

+828
-0
lines changed

2021/day13.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import numpy as np
2+
eg = """6,10
3+
0,14
4+
9,10
5+
0,3
6+
10,4
7+
4,11
8+
6,0
9+
6,12
10+
4,1
11+
0,13
12+
10,12
13+
3,4
14+
3,0
15+
8,4
16+
1,10
17+
2,14
18+
8,10
19+
9,0
20+
21+
fold along y=7
22+
fold along x=5
23+
"""
24+
25+
def parse(eg):
26+
numbers,instructions = eg.split("\n\n")
27+
numbers = np.fromiter(numbers.replace("\n", ",").split(","), int).reshape([-1,2])
28+
#print(numbers)
29+
grid = np.zeros(shape=1+np.amax(numbers, axis=0), dtype=int)
30+
t = np.transpose(numbers)
31+
grid[t[0],t[1]] = 1
32+
instructions = [(i[11], int(i[13:])) for i in filter(None, instructions.split('\n'))]
33+
# print(grid)
34+
print(instructions)
35+
return np.transpose(grid), instructions
36+
37+
def fold(grid, instructions, limit=10^6):
38+
#print("grid\n{}".format(grid))
39+
for i,(direction,pos) in enumerate(instructions):
40+
#print(i)
41+
if direction == 'y':
42+
top = grid[0:pos,:]
43+
bot = np.flipud(grid[pos+1:,:])
44+
#print("top\n{}".format(top))
45+
#print("bottom\n{}".format(bot))
46+
grid = top+bot
47+
else:
48+
left = grid[:,0:pos]
49+
right = np.fliplr(grid[:,pos+1:])
50+
#print("left\n{}".format(left))
51+
#print("right\n{}".format(right))
52+
grid = left+right
53+
grid[grid>1] = 1
54+
#print("combined\n{}".format(grid))
55+
print("{} dots visible".format(np.sum(grid)))
56+
if i>= limit:
57+
break
58+
return(grid)
59+
60+
fold(*parse(eg))
61+
fold(*parse(open('input/day13.txt').read()), limit=1)
62+
63+
g = fold(*parse(open('input/day13.txt').read()))
64+
with np.printoptions(threshold=np.inf):
65+
print(g)
66+
67+
# [[1 1 1 1 0 1 1 1 1 0 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0]
68+
# [1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0]
69+
# [1 1 1 0 0 1 1 1 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 0]
70+
# [1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 0 1 1 1 0 0 1 0 0 0 0]
71+
# [1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 0 1 0 0 0 0]
72+
# [1 1 1 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 1 1 1 0 1 0 0 1 0 1 0 0 0 0]]
73+
74+
# EFLFJGRF

0 commit comments

Comments
 (0)