-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.jl
93 lines (82 loc) · 2.22 KB
/
day15.jl
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
grid = open("chiton.txt") do f
hcat(map(row -> parse.(Int, split(row, "")), readlines(f))...)
end
function dijkstra(graph, source, target)
Q = []
dist = Dict()
prev = Dict()
for v in CartesianIndices(graph)
dist[v] = Inf
prev[v] = nothing
push!(Q, v)
end
dist[source] = 0
while !isempty(Q)
u = argmin(x -> dist[x], Q)
# println("considering $(u)")
filter!(x -> x != u, Q)
if u == target
break
end
x, y = Tuple(u)
# x_range = max(x - 1, 1):min(x + 1, size(graph, 1))
# y_range = max(y - 1, 1):min(y + 1, size(graph, 2))
# # println("considering neighbors $(x_range), $(y_range)")
# neighbors = filter(x -> x in Q, CartesianIndices(graph)[x_range, y_range])
neighbors = []
if x > 1
push!(neighbors, CartesianIndex(x-1, y))
end
if y > 1
push!(neighbors, CartesianIndex(x, y-1))
end
if x < size(graph, 1)
push!(neighbors, CartesianIndex(x+1, y))
end
if y < size(graph, 2)
push!(neighbors, CartesianIndex(x, y+1))
end
# println(neighbors)
for v in neighbors
# println("considering neighbor $(v), $(dist[u] + graph[v]) < $(dist[v])")
alt = dist[u] + graph[v]
if alt < dist[v]
dist[v] = alt
prev[v] = u
end
end
end
return dist, prev
end
function shortest_path(prev, source, target)
S = []
u = target
if prev[u] !== nothing || u == source
while u !== nothing
pushfirst!(S, u)
u = prev[u]
end
end
return S
end
source = CartesianIndex(1,1)
target = CartesianIndex(size(grid, 1), size(grid, 2))
dist, prev = dijkstra(grid, source, target)
S = shortest_path(prev, source, target)
# for i in 1:size(grid, 2)
# for j in 1:size(grid, 1)
# if CartesianIndex(j, i) in S
# print('X')
# else
# print(grid[j, i])
# # print(' ')
# end
# end
# println()
# end
sum = 0
for i in filter(x -> x != source, S)
global sum, grid
sum += grid[i]
end
println(sum)