Advent of code Day 19

Goal: Nice Math Game - The Josephus Problem Source : https://www.youtube.com/watch?v=uCsD3ZGzMgE

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


In [1]:
elfs <- 3018458

Used the algo given in the video


In [2]:
Q1 <- (elfs-2^(floor(log(elfs, base = 2)))+1)*2-1
Q1


1842613

Used the logic to detect patern in the video to find the logic with this one


In [3]:
maxNumber <- floor(log(elfs, base = 3))
root <- 3^maxNumber
rest <- elfs-root
if(rest==0){
  Q2 <- elfs
} else {
  Q2 <- min(root,rest)+max(0,rest-root)*2
}
Q2


1424135

In [4]:
cat("Q1- If you give take to the elf on your left, the remaining elf is : ", Q1, '\n')
cat("Q2- If you give take to the elf facing you, the remaining elf is : ", Q2, '\n')


Q1- If you give take to the elf on your left, the remaining elf is :  1842613 
Q2- If you give take to the elf facing you, the remaining elf is :  1424135