Problem 79

Problem 79 - Project Euler
長さは分からないパスコードの中から順番はそのままでランダムに3つの文字を抽出したリストがあるので、そのリストを元にパスコードを復元せよというような問題。
きっと色々な解き方がある。

## 左から
keylog <- read.table("keylog.txt")[[1]]
ans <- numeric(0)
while(length(keylog) > 1){
  lefts <- unique(as.numeric(substr(keylog,1,1)))
  centors <- unique(as.numeric(substr(keylog,2,2)))
  if(length(x <- setdiff(lefts,centors))!=1) break
  ans <- c(ans,x)            
  keylog <- keylog[-grep(x,keylog)]
}
c(ans,keylog)

## 右から
keylog <- read.table("keylog.txt")[[1]]
ans <- numeric(0)
while(length(keylog) > 1){
  rights <- unique(as.numeric(substr(keylog,3,3)))
  centors <- unique(as.numeric(substr(keylog,2,2)))
  if(length(x <- setdiff(rights,centors))!=1) break
  ans <- c(ans,x)
  keylog <- keylog[-grep(x,keylog)]
}
c(keylog,rev(ans))