We could create a list of possible times. We want to then find the minimum time that is larger than departure time but that’s just modulo. But if we do modulo, we’re finding the time before departure time so we can adjust and get times for each bus time
What is the earliest timestamp such that all of the listed bus IDs depart at offsets matching their positions in the list?
# part twobusses_2 <-read_csv(my_file, skip =1, col_names =FALSE, col_types =cols(.default =col_character())) %>%pivot_longer(cols =everything()) %>%mutate(value =if_else(value =="x", "0", value), value =as.double(value),time_diff =row_number()-1) %>%select(-name) %>%filter(value !=0)# initial info first_bus <- busses_2$value[1]num_bus <-nrow(busses_2)
Warning! There might be much searching.
# info for the looptime_step <- first_busmy_answer <-0# https://stackoverflow.com/questions/25946047/how-to-prevent-scientific-notation-in-roptions(scipen=999)# for each of the bussesfor (b inseq(2, num_bus)) {# print for helpprint(paste("The bus number is ", b))# nicer looking than referring to indices all the time b_time = busses_2$value[[b]] b_diff = busses_2$time_diff[[b]]# stop looking once we have found an answer found =FALSE x =0# loop the loopwhile (found !=TRUE) {# t is always multiple of first bus time t = my_answer + time_step * x # this is when the next bus will arrive checker = t + b_diff if (checker %% b_time ==0) {# check progressprint(t)# found so go to next bus found =TRUE# update answer my_answer = t# increase search steps time_step = time_step * b_time } # increment search steps x = x +1 }}
[1] "The bus number is 2"
[1] 104
[1] "The bus number is 3"
[1] 185770
[1] "The bus number is 4"
[1] 850993
[1] "The bus number is 5"
[1] 19698978
[1] "The bus number is 6"
[1] 1165656466
[1] "The bus number is 7"
[1] 34398423618
[1] "The bus number is 8"
[1] 49152428274274
[1] "The bus number is 9"
[1] 552612234243498
# # # this is too much searching# reckon_minimum <- 100000000000000# # max_bus_time <- max(busses_2$value)# max_bus_diff <- busses_2$time_diff[which(busses_2$value == max_bus_time)]# # while (x < 1e16) {# poss_bus = 0# check_num = (max_bus_time * x - max_bus_diff)# for (y in seq(1,num_bus)) {# if ((check_num + busses_2$time_diff[[y]]) %% busses_2$value[[y]] != 0) {# # this is not the droid you're looking for# break# } else {# poss_bus = poss_bus + 1# }# }# if (poss_bus == 4) {# print(paste(check_num, poss_bus))# break# }# x = x + 1# }#