Created
September 3, 2024 21:38
-
-
Save alejandrohagan/912cafd3b2efc20f22dbab2d78619bf4 to your computer and use it in GitHub Desktop.
2023-09-03-test-your-intuition-56-fifteen-boxes-puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## riddle here https://gilkalai.wordpress.com/2024/09/03/test-your-intuition-56-fifteen-boxes-puzzle/ | |
library(tidyverse) | |
choose_box <- function(){ | |
# create search order | |
andrew <- 1:15 | |
barbara <- c(1,6,11,2,7,12,3,8,13,4,9,14,5,10,15) | |
## assign prize location | |
prize1 <- sample(1:15,1) | |
prize2 <- sample(1:15,1) | |
## assign prize discovery location by pattern | |
box1_prize1_res <- which(andrew==prize1) | |
box2_prize1_res <- which(barbara==prize1) | |
box1_prize2_res <- which(andrew==prize2) | |
box2_prize2_res <- which(barbara==prize2) | |
## asign prize winners | |
if(box2_prize1_res==box1_prize1_res){ | |
prize1_win <- "tie" | |
}else if(box1_prize1_res<box2_prize1_res){ | |
prize1_win <- "andrew" | |
}else{ | |
prize1_win <- "barbara" | |
} | |
if(box2_prize2_res==box1_prize2_res){ | |
prize2_win <- "tie" | |
}else if(box1_prize2_res<box2_prize2_res){ | |
prize2_win <- "andrew" | |
}else{ | |
prize2_win <- "barbara" | |
} | |
## probably a better way to do this but brain can't think right now | |
## assign winner of each prize and game | |
out <- tibble( | |
prize=c("prize1","prize2") | |
,winner=c(prize1_win,prize2_win) | |
) |> | |
mutate( | |
andrew=if_else(winner=="barbara",0,1) | |
,barbara=if_else(winner=="andrew",0,1) | |
) |> | |
janitor::adorn_totals() |> | |
as_tibble() |> | |
filter(prize=="Total") |> | |
mutate( | |
game_winner= | |
case_when( | |
andrew>barbara~"Andrew" | |
,barbara>andrew~"Barbara" | |
,.default = "Tie" | |
) | |
) |> pull(game_winner) | |
return(out) | |
} | |
results <- tibble(x=replicate(1000,choose_box())) | |
results |> count(x,sort = TRUE) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment