-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.R
164 lines (124 loc) · 3.89 KB
/
day15.R
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
## READ AND PREPARE DATA -------------------------------------------------------
day15 <- readLines("inputs/input15.txt")
day15 <- as.numeric(unlist(strsplit(day15, "")))
day15 <- matrix(day15, nrow = 100)
# ## EXAMPLE
# day15 <- c(
# "1163751742138137367221365113283694931569746341711113191281371359912421312542163912931385212311944581"
# )
#
# day15 <- as.numeric(unlist(strsplit(day15, "")))
#
# day15 <- matrix(day15, nrow = 10, byrow = TRUE)
## FUNCTIONS -------------------------------------------------------------------
get_neighbours <- function(mat, idx) {
row <- idx %% nrow(mat)
if (row == 0) row <- nrow(mat)
col <- ceiling(idx/ncol(mat))
neighbours <-
data.frame(
row = c(row, row, row + 1, row - 1),
col = c(col + 1, col - 1, col, col)
)
# Exclude values outside of matrix
neighbours <-
neighbours[neighbours$row %in% 1:nrow(mat) & neighbours$col %in% 1:ncol(mat), ]
# Turn into vector positions
neighbours <- neighbours$row + nrow(mat) * (neighbours$col - 1)
return(neighbours)
}
## PART 1 ----------------------------------------------------------------------
timer <- Sys.time()
costs <- rep(Inf, nrow(day15) * ncol(day15))
complete <- rep(FALSE, nrow(day15) * ncol(day15))
temp_node <- 1
costs[1] <- 0
complete[1] <- TRUE
temp_costs <- costs[temp_node]
next_nodes <- get_neighbours(day15, temp_node)
# Update costs
costs[next_nodes] <-
ifelse(
costs[next_nodes] > temp_costs + day15[next_nodes],
temp_costs + day15[next_nodes],
costs[next_nodes]
)
# Next node: Node with lowest costs that has not been completed yet
temp_node <- which(costs == min(costs[!complete]))[1]
while (!all(complete)) {
temp_costs <- costs[temp_node]
next_nodes <- get_neighbours(day15, temp_node)
next_nodes <- next_nodes[!complete[next_nodes]]
costs[next_nodes] <-
ifelse(
costs[next_nodes] > temp_costs + day15[next_nodes],
temp_costs + day15[next_nodes],
costs[next_nodes]
)
complete[temp_node] <- TRUE
if (all(complete)) break
min_costs <- min(costs[!complete])
temp_node <- which(costs == min_costs & !complete)
temp_node <- temp_node[1]
costs
}
Sys.time() - timer
costs[length(costs)]
## PART 2 ----------------------------------------------------------------------
#################################################
## HEADS UP: TAKES IN MIN TO RUN ON MY LAPTOP! ##
#################################################
# Create full map
full_map <- day15
# Extend downwards:
for (i in 1:4) {
to_add <- day15 + i
to_add <- to_add %% 9
to_add[to_add == 0] <- 9
full_map <- rbind(full_map, to_add)
}
previous_map <- full_map
# Extend to the right:
for (i in 1:4) {
to_add <- previous_map + i
to_add <- to_add %% 9
to_add[to_add == 0] <- 9
full_map <- cbind(full_map, to_add)
}
# Run algorithm
timer <- Sys.time()
costs <- rep(Inf, nrow(full_map) * ncol(full_map))
complete <- rep(FALSE, nrow(full_map) * ncol(full_map))
temp_node <- 1
costs[1] <- 0
complete[1] <- TRUE
temp_costs <- costs[temp_node]
next_nodes <- get_neighbours(full_map, temp_node)
# Update costs
costs[next_nodes] <-
ifelse(
costs[next_nodes] > temp_costs + full_map[next_nodes],
temp_costs + full_map[next_nodes],
costs[next_nodes]
)
# Next node: Node with lowest costs that has not been completed yet
temp_node <- which(costs == min(costs[!complete]))[1]
while (!all(complete)) {
temp_costs <- costs[temp_node]
next_nodes <- get_neighbours(full_map, temp_node)
next_nodes <- next_nodes[!complete[next_nodes]]
costs[next_nodes] <-
ifelse(
costs[next_nodes] > temp_costs + full_map[next_nodes],
temp_costs + full_map[next_nodes],
costs[next_nodes]
)
complete[temp_node] <- TRUE
if (all(complete)) break
min_costs <- min(costs[!complete])
temp_node <- which(costs == min_costs & !complete)
temp_node <- temp_node[1]
costs
}
Sys.time() - timer
costs[length(costs)]