Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Last active May 26, 2018 13:22
Show Gist options
  • Save Torvaney/caccdba1b3639617bfadc1b14e0dfb1b to your computer and use it in GitHub Desktop.
Save Torvaney/caccdba1b3639617bfadc1b14e0dfb1b to your computer and use it in GitHub Desktop.
library(tidyverse)
cyclist_speed <- 18/60
motorcycle_delay <- 1
hill_length <- 10 # Doesn't matter (it will cancel out)
simulate_uphill <- function(motorcycle_speed) {
time_to_finish <- hill_length / cyclist_speed
time_motorcycles <- hill_length / motorcycle_speed
n_motorcycles <- ((time_to_finish - motorcycle_delay) - time_motorcycles) %/% 1
n_motorcycles
}
simulate_downhill <- function(motorcycle_speed) {
time_to_finish <- hill_length / (2 * cyclist_speed)
time_motorcycles <- hill_length / motorcycle_speed
n_motorcycles <- (time_to_finish %/% 1) + (time_motorcycles %/% 1)
n_motorcycles
}
simulate_cyclist <- function(motorcycle_speed) {
abs(simulate_downhill(motorcycle_speed/60) - simulate_uphill(motorcycle_speed/60))
}
optimise(simulate_cyclist, lower = 0, upper = 100)
tibble(x = 40:100,
y = simulate_cyclist(40:100)) %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
scale_x_continuous(breaks = seq(0, 100, 10),
minor_breaks = seq(0, 100, 5)) +
labs(title = "Motorcycle puzzle",
subtitle = "Difference in # of motorcycles passed up vs down",
y = "",
x = "Speed of motorcycle")
# Solution = 72
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment