Last updated: 2017-12-14
Code version: aa59a2a
sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] magrittr_1.5 ggplot2_2.2.1.9000 withr_2.1.0.9000 jsonlite_1.5 igraph_1.1.2
[6] rlist_0.4.6.1 bindrcpp_0.2 aocodeR_0.1.1 testthat_1.0.2 forcats_0.2.0
[11] stringr_1.2.0 dplyr_0.7.4 purrr_0.2.4 readr_1.1.1 tidyr_0.7.2
[16] tibble_1.3.4 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.14 lubridate_1.7.1 lattice_0.20-35 assertthat_0.2.0
[5] rprojroot_1.2 digest_0.6.12 packrat_0.4.8-1 psych_1.7.8
[9] R6_2.2.2 cellranger_1.1.0 plyr_1.8.4 backports_1.1.1
[13] evaluate_0.10.1 httr_1.3.1 BiocInstaller_1.26.0 rlang_0.1.4
[17] lazyeval_0.2.1 curl_3.0 readxl_1.0.0 rstudioapi_0.7
[21] data.table_1.10.4-3 Matrix_1.2-12 rmarkdown_1.8 devtools_1.13.4
[25] foreign_0.8-69 munsell_0.4.3 broom_0.4.2 compiler_3.4.2
[29] modelr_0.1.1 pkgconfig_2.0.1 base64enc_0.1-3 mnormt_1.5-5
[33] htmltools_0.3.6 crayon_1.3.4 grid_3.4.2 nlme_3.1-131
[37] gtable_0.2.0 git2r_0.19.0 scales_0.5.0.9000 cli_1.0.0
[41] stringi_1.1.6 reshape2_1.4.2 xml2_1.1.1 tools_3.4.2
[45] glue_1.2.0 hms_0.4.0 rsconnect_0.8.5 parallel_3.4.2
[49] yaml_2.1.15 colorspace_1.3-2 rvest_0.3.2 memoise_1.1.0
[53] knitr_1.17 bindr_0.1 haven_1.1.0
— Day 12: Digital Plumber —
Walking along the memory banks of the stream, you find a small village that is experiencing a little confusion: some programs can’t communicate with each other.
Programs in this village communicate using a fixed system of pipes. Messages are passed between programs using these pipes, but most programs aren’t connected to each other directly. Instead, programs pass messages between each other until the message reaches the intended recipient.
For some reason, though, some of these messages aren’t ever reaching their intended recipient, and the programs suspect that some pipes are missing. They would like you to investigate.
You walk through the village and record the ID of each program and the IDs with which it can communicate directly (your puzzle input). Each program has one or more programs with which it can communicate, and these pipes are bidirectional; if 8 says it can communicate with 11, then 11 will say it can communicate with 8.
You need to figure out how many programs are in the group that contains program ID 0.
For example, suppose you go door-to-door like a travelling salesman and record the following list:
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5
In this example, the following programs are in the group that contains program ID 0:
Program 0 by definition. Program 2, directly connected to program 0. Program 3 via program 2. Program 4 via program 2. Program 5 via programs 6, then 4, then 2. Program 6 via programs 4, then 2.
Therefore, a total of 6 programs are in this group; all but program 1, which has a pipe that connects it to itself.
How many programs are in the group that contains program ID 0?
library(tidyverse)
library(testthat)
library(aocodeR)
library(igraph)
library(magrittr)
input <- aoc_get_input(day = 12, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
"/secrets/session_cookie.txt"))
e_df <- function(x) {
x <- x %>% strsplit(.," <-> ") %>% unlist %>% strsplit(.,", ?")
tibble(n1 = x[[1]], n2 = x[[2]])
}
to_g <- function(input) {
input %>%
str_split("\n") %>% unlist %>%
map_dfr(e_df) %>%
graph_from_data_frame(directed = F)
}
group_n <- function(input, v = "0") {
g <- to_g(input)
distances(g, v = v, to=V(g), weights=NA) %>%
is.infinite() %>% `!` %>% sum
}
test_input <- "0 <-> 2\n1 <-> 1\n2 <-> 0, 3, 4\n3 <-> 2, 4\n4 <-> 2, 3, 6\n5 <-> 6\n6 <-> 4, 5"
expect_equal(group_n(test_input),6)
group_n(test_input)
[1] 6
test_input %>% to_g %>% plot
group_n(input)
[1] 141
g <- input %>% to_g
#expect_equal(,)
input %>% to_g %>% clusters %>% `$`("no")
[1] 171