-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.R
46 lines (29 loc) · 960 Bytes
/
day25.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
library(igraph)
# Example input, if needed
# day25 <- readLines("./puzzle_input/input_day25_exp.txt")
day25 <- readLines("./puzzle_input/input_day25.txt")
day25 <- strsplit(day25, ": ")
from <- sapply(day25, function(x) x[1])
to <- lapply(day25, function(x) unlist(strsplit(x[2], " ")))
vertices <- data.frame(from = c(), to = c())
for (i in seq_along(from)) {
for (j in seq_along(to[[i]])) {
vertices <-
rbind.data.frame(
vertices,
data.frame(
from = from[i],
to = to[[i]][j]
)
)
}
}
## PART 1 ----------------------------------------------------------------------
start <- Sys.time()
nw <- graph_from_edgelist(as.matrix(vertices), directed = FALSE)
# Delete the three edges with the highest betweenness
e_btw <- edge_betweenness(nw)
cut_nw <- delete.edges(nw, E(nw)[order(e_btw, decreasing = TRUE)][1:3])
prod(components(cut_nw)$csize)
# 507626
Sys.time() - start