-
Notifications
You must be signed in to change notification settings - Fork 0
/
asana.py
59 lines (43 loc) · 1.37 KB
/
asana.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
import itertools
DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0)]
'''
There are 2 parts to the solution:
- find the center of the garden, where the rabbit initially is
- find the maximal path from there onwards until it reaches a square from which it can't move
'''
def carrot_count(garden):
# makes our accesses to the garden matrix cleaner
smart_matrix = {
(x, y): val
for y, row in enumerate(garden)
for x, val in enumerate(row)
}
# Find the center of the matrix
h = len(garden)
w = len(garden[0])
x = [w // 2]
if w % 2 == 0:
x.append(x[0] - 1)
y = [h // 2]
if h % 2 == 0:
y.append(y[0] - 1)
# find the square closest to the center with the highest carrot count
center = max(
itertools.product(x, y),
key=lambda ij: smart_matrix[ij],
)
carrots = 0
location = center
# Move up/down/left/right based on carrot count
while smart_matrix[location]:
carrots += smart_matrix[location]
smart_matrix[location] = 0
x, y = location
adj = [(x + px, y + py) for (px, py) in DIRECTIONS]
location = max(adj, key=lambda xy: smart_matrix.get(xy, 0))
# return the maximal carrots count
return carrots
print carrot_count([[5, 7, 8, 6, 3],
[0, 0, 7, 0, 4],
[4, 6, 3, 4, 9],
[3, 1, 0, 5, 8]])