-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
48 lines (36 loc) · 1.33 KB
/
day3.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
import numpy as np
f = open('input3.txt','r')
txt = f.read()
txt = txt.split('\n')
puzzle_input = txt
def list2mat(lst):
mat = np.zeros((len(lst), len(lst[0])))
for i in range(len(lst)):
for j in range(len(lst[0])):
if lst[i][j] == '.':
mat[i,j] = 0
elif lst[i][j] == '#':
mat[i,j] = 1
return mat
input_mat = list2mat(puzzle_input)
# duplicate from the right till the number of columns is 3x the number of rows:
input_mat_large = np.tile(input_mat, (1, int(np.ceil(3*(input_mat.shape[0]/input_mat.shape[1])))))
# go down slope and count trees:
def go_down_slope(input_mat, slope_xy):
x_pos = 0
y_pos = 0
cnt_trees = 0
while y_pos<input_mat.shape[0]-1:
x_pos += slope_xy[0]
y_pos += slope_xy[1]
if input_mat[y_pos, x_pos]==1:
cnt_trees+=1
return cnt_trees
res1 = go_down_slope(input_mat_large, [3,1])
print(f'part 1 result = {res1}')
slope_list = [[1,1],[3,1],[5,1],[7,1],[1,2]]
trees = np.zeros((len(slope_list)))
for ind,sl in enumerate(slope_list):
input_mat_large = np.tile(input_mat, (1, int(np.ceil((sl[0]/sl[1])*(input_mat.shape[0]/input_mat.shape[1])))))
trees[ind] = go_down_slope(input_mat_large, sl)
print(f'part 2 result = {int(np.prod(trees))}')