Advent of code Day 18

Goal: play with string in a game of live

Ref: http://adventofcode.com/2016/day/18


In [1]:
library(stringr)

In [2]:
inputString <- "^.....^.^^^^^.^..^^.^.......^^..^^^..^^^^..^.^^.^.^....^^...^^.^^.^...^^.^^^^..^^.....^.^...^.^.^^.^"
sizeQ1 <- 40
sizeQ2 <- 400000

In [3]:
computeTheShit <- function(input, size){
    latest <- c(".", strsplit(input,"")[[1]], "." )
    total <- list(latest)
    while(length(total)<size){
        tmp <- "."
        for(i in 2:(length(latest)-1)){
            #tmp <- c(tmp,latest[i])
            upper <- paste(latest[(i-1):(i+1)],collapse="")
            #print(upper)
            switch(upper, 
            "^^."={
              tmp <- c(tmp,'^')
            },
            ".^^"={
              tmp <- c(tmp,'^')  
            },
            "^.."={
              tmp <- c(tmp,'^')  
            },
            "..^"={
              tmp <- c(tmp,'^')  
            },
            {
               tmp <- c(tmp,'.')
            }
            )
        }
        tmp <- c(tmp,".")
        total[[length(total)+1]] <- tmp
        latest <- tmp
    }    
    return(sum(str_count(total,"\\."))-length(total)*2)#sum(str_count(total,"\\."))-length(total)*2
}

In [4]:
Q1 <- computeTheShit(inputString, sizeQ1)
Q1


1974

In [5]:
Q2 <- computeTheShit(inputString, sizeQ2)
Q2


19991126

In [6]:
cat("Q1- Shortest path to the vault is : ", Q1, '\n')
cat("Q2- Longest path to the vault is : ", Q2, '\n')


Q1- Shortest path to the vault is :  1974 
Q2- Longest path to the vault is :  19991126