Advent of code Day 16

Goal: Creating carbage Data and counting hash

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


In [1]:
library(stringr)
library(plyr)

In [2]:
Q1original <- as.integer(str_split("01111010110010011","")[[1]])
Q1originalLength <- 272

In [3]:
Q2original <- as.integer(str_split("01111010110010011","")[[1]])
Q2originalLength <- 35651584

In [4]:
slice <- function(input, by=2){ 
  starts <- seq(1,length(input),by)
  tt <- lapply(starts, function(y) input[y:(y+(by-1))])
  llply(tt, function(x) x[!is.na(x)])
}

In [5]:
processData <- function(original ,originalLength){
    data <- original
    while(length(data)<originalLength){
        a <- data
        b <- a
        b <- rev(b)
        b <- (b+1)%%2
        data <- c(a,c(0),b)
    }

    officialData <- data[0:originalLength]
    chkSum <- officialData
    ll <- length(chkSum)

    while(ll%%2==0){
        ll <- ll/2
    }
    awnser <- slice(chkSum, length(chkSum)/ll)
    resultQ1 <- sapply(awnser,function(x) {(sum(x)+1)%%2})
    paste(resultQ1,collapse="")
}

In [6]:
Q1 <- processData(Q1original,Q1originalLength)
Q1


"00100111000101111"

In [7]:
Q2 <- processData(Q2original,Q2originalLength)
Q2


"11101110011100110"

In [8]:
cat("Q1 - The Checksum for the first dataset is = ", Q1, '\n')
cat("Q2 - The Checksum for the second dataset is = ", Q2, '\n')


Q1 - The Checksum for the first dataset is =  00100111000101111 
Q2 - The Checksum for the second dataset is =  11101110011100110