Advent of Code: Day 12

less than 1 minute read

Parts 1 and 2

raw_input <- "0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5"

cat(raw_input, file = "advent-data/2017-12-12-advent-day12-test.txt")


get_n_program <- function(input) {
    d <- readLines(input, warn = FALSE) %>%
        strsplit(" <-> ") %>% 
        map_df(function(x) tibble(origin = x[1], dest = x[2])) %>%
        mutate(dest = strsplit(dest, ",")) %>%
        unnest() %>%
        mutate_all(~ gsub("\\s", "", .))
    g <- graph_from_data_frame(d)
    list(n_prgs = ego_size(g, vcount(g),  nodes="0"),
         n = components(g)$no)
}

get_n_program("advent-data/2017-12-12-advent-day12-test.txt")
## $n_prgs
## [1] 6
## 
## $n
## [1] 2
get_n_program("advent-data/2017-12-12-advent-day12.txt")
## $n_prgs
## [1] 115
## 
## $n
## [1] 221

Comments