Last updated: 2017-12-14

Code version: aa59a2a

See more puzzles

Advent of Code

Session information

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         

Brief

— 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?

Let’s go

Packages & functions

library(tidyverse)
library(testthat)
library(aocodeR)
library(igraph)
library(magrittr)

Input

input <- aoc_get_input(day = 12, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
                                                 "/secrets/session_cookie.txt"))
 

Functions

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

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

deploy

group_n(input)
[1] 141
g <- input %>% to_g 

plot

Success!



—- Part 2 —-

Brief

Let’s go

Test

#expect_equal(,)

deploy

input %>% to_g %>% clusters %>% `$`("no")
[1] 171

Success!



template based on the workflowr standalone template

LS0tCnRpdGxlOiAiLS0tIERheSAxMjogRGlnaXRhbCBQbHVtYmVyIC0tLSIKYXV0aG9yOiAiYW5uYWtyeXN0YWxsaSIKZGF0ZTogMjAxNy0xMi0xMgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKZWRpdG9yX29wdGlvbnM6IAogIGNodW5rX291dHB1dF90eXBlOiBpbmxpbmUKLS0tCgpgYGB7ciBrbml0ci1vcHRzLWNodW5rLCBpbmNsdWRlPUZBTFNFfQojIFVwZGF0ZSBrbml0ciBjaHVuayBvcHRpb25zCiMgaHR0cHM6Ly95aWh1aS5uYW1lL2tuaXRyL29wdGlvbnMvI2NodW5rLW9wdGlvbnMKa25pdHI6Om9wdHNfY2h1bmskc2V0KAogIGNvbW1lbnQgPSBOQSwKICBmaWcuYWxpZ24gPSAiY2VudGVyIiwKICB0aWR5ID0gRkFMU0UsCiAgZmlnLnBhdGggPSBwYXN0ZTAoImZpZ3VyZS8iLCBrbml0cjo6Y3VycmVudF9pbnB1dCgpLCAiLyIpCikKYGBgCgpgYGB7ciBsYXN0LXVwZGF0ZWQsIGVjaG89RkFMU0UsIHJlc3VsdHM9J2FzaXMnLCB3YXJuaW5nPUZBTFNFfQojIEluc2VydCB0aGUgZGF0ZSB0aGUgZmlsZSB3YXMgbGFzdCB1cGRhdGVkCmNhdChzcHJpbnRmKCIqKkxhc3QgdXBkYX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