Skip to content

Instantly share code, notes, and snippets.

@adisarid
Created February 10, 2024 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adisarid/b18fd3ab2f5684943cb5e935567cfd40 to your computer and use it in GitHub Desktop.
Save adisarid/b18fd3ab2f5684943cb5e935567cfd40 to your computer and use it in GitHub Desktop.
solution to bar-ilan math riddle #2
"000"
"00O"
"0O0"
"0OO"
"O0O"
"OO0"
"OOO"
series_length <- 42
series <- rep(T, series_length)
total_steps <- 0
paste0(c(series*1, " <- (not counted)"), collapse = "")
while (sum(series) > 0){
i <- series_length # initialize to series length
# make sure you are able to operate on series (game incomplete)
not_moved <- T
while ((i > 0) & (sum(series) > 0) & not_moved){
# verify access by value (true)
if (series[i]) {
series[i] <- F # turn off value
total_steps <- total_steps + 1 # count this as a step
not_moved <- F # this is considered a move reset count
# if not on edge, switch next value
if (i < series_length){
series[i+1] <- !series[i+1]
}
cat(paste0(c(series*1, " (", total_steps, ")"), collapse = ""), "\n")
} else {
# if the current pointer is false, proceed left
i <- i-1
}
}
}
paste0(c(series*1, " <- (not counted)"), collapse = "")
cat(glue::glue("The total number of steps is {total_steps}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment