Advent of code Day 14

Goal: MD5 playing in a lovely loop

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


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

In [2]:
input <- "zpqevtbw"

In [3]:
charwith3 <- data.frame(input=integer(),
                        md5=character(), 
                        ch=character(),
                        valid=logical(), 
                        stringsAsFactors=FALSE) 

charwith5 <- data.frame(input=integer(),
                        md5=character(), 
                        ch=character(), 
                        stringsAsFactors=FALSE) 

current <- 0
goal <- 64
finish <- 99999999
done <- FALSE
while(finish>=current){
  if(sum(charwith3$valid)>=goal && finish==99999999){
    finish <- current + 1003
  }
  x <- paste(input,current,sep="")
  
  x <- digest(x,"md5", serialize = FALSE)
  
  #look for 5
  search <- TRUE
  aa<-x
  ttt <- str_match(aa,"(.)\\1{4,}")
  if((!is.na(ttt[1]))){
    cht <- ttt[2]
    charwith5<-rbind(charwith5,data.frame(input=current,md5=x,ch=cht,valid=FALSE))
    charwith3$valid[charwith3$ch==cht & charwith3$input>current-1001] <- TRUE
    charwith3$solver[charwith3$ch==cht & charwith3$input>current-1001] <- current
  }
  
  #Look for 3
  aa<-x
  ttt <- str_match(aa,"(.)\\1{2,}")
  if((!is.na(ttt[1]))){
    cht <- ttt[2]
    charwith3<-rbind(charwith3,data.frame(input=current,md5=x,ch=cht,solver=0,valid=FALSE))
  }
  
  
  current <- current+1
}

In [4]:
Q1 <- charwith3$input[charwith3$valid][64]

In [5]:
charwith3 <- data.frame(input=integer(),
                        md5=character(), 
                        ch=character(),
                        valid=logical(), 
                        stringsAsFactors=FALSE) 

charwith5 <- data.frame(input=integer(),
                        md5=character(), 
                        ch=character(), 
                        stringsAsFactors=FALSE) 

current <- 0
goal <- 64
finish <- 99999999
done <- FALSE
while(finish>=current){
  if(sum(charwith3$valid)>=goal && finish==99999999){
    finish <- current + 1001
  }
  x <- paste(input,current,sep="")
  
  for (i in 1:2017) {
    x <- digest(x,"md5", serialize = FALSE)
}
  #look for 5
  search <- TRUE
  aa<-x
  ttt <- str_match(aa,"(.)\\1{4,}")
  if((!is.na(ttt[1]))){
    cht <- ttt[2]
    charwith5<-rbind(charwith5,data.frame(input=current,md5=x,ch=cht,valid=FALSE))
    charwith3$valid[charwith3$ch==cht & charwith3$input>current-1001] <- TRUE
    charwith3$solver[charwith3$ch==cht & charwith3$input>current-1001] <- current
  }
  
  #Look for 3
  aa<-x
  ttt <- str_match(aa,"(.)\\1{2,}")
  if((!is.na(ttt[1]))){
    cht <- ttt[2]
    charwith3<-rbind(charwith3,data.frame(input=current,md5=x,ch=cht,solver=0,valid=FALSE))
  }
  
  
  current <- current+1
}

In [6]:
Q2 <- charwith3$input[charwith3$valid][64]

In [7]:
cat("Q1 - The 64th key is produce by the index = ", Q1, '\n')
cat("Q2 - The 64th key is produce by the index = ", Q2, '\n')


Q1 - The 64th key is produce by the index =  16106 
Q2 - The 64th key is produce by the index =  22423