-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
77 lines (61 loc) · 2.02 KB
/
day9.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
from collections import Counter, defaultdict
with open('day9_input.txt') as f:
input_array = f.read().split('\n')
input_array = list(map(lambda x: [int(y) for y in x], input_array))
def first_part_func(array):
result = 0
for i, line in enumerate(array):
for j in range(len(line)):
point = line[j]
up = True
down = True
left = True
right = True
if i == 0:
up = False
if i == len(array) - 1:
down = False
if j == 0:
left = False
if j == len(line) - 1:
right = False
if up and point >= array[i - 1][j]:
continue
if down and point >= array[i + 1][j]:
continue
if left and point >= line[j - 1]:
continue
if right and point >= line[j + 1]:
continue
result += point + 1
print(result)
def get_grid(arr):
c = defaultdict()
for y in range(len(arr)):
c[(-1, y)] = 0
c[(len(arr[0]), y)] = 0
for x in range(len(arr[0])):
c[(x, -1)] = 0
c[(x, len(arr))] = 0
for y, line in enumerate(arr):
for x, value in enumerate(line):
c[(x, y)] = value
return c
def second_part_func(array):
counter = Counter()
coords = get_grid(array)
for (x, y), value in coords.items():
if 0 <= x < len(array[0]) and 0 <= y < len(array) and value != 9:
while 0 <= x < len(array[0]) and 0 <= y < len(array):
for step_x, step_y in ((1, 0), (-1, 0), (0, 1), (0, -1)):
if coords[x + step_x, y + step_y] < coords[x, y]:
x += step_x
y += step_y
break
else:
counter[x, y] += 1
break
a, b, c = counter.most_common(3)
print(a[1] * b[1] * c[1])
first_part_func(input_array)
second_part_func(input_array)