How to calculate time between two different times?

someone was trying to hint that if you separate the time in hours and minutes, instead of separate the string in all its characters, it is much easier to work with it

people are willing to help
if you don’t understand an hint say so
say “I don’t understand what you are hinting”
people try to be the vaguest possible
while giving something useful so that you do the most legwork, and if you need something more you need to say so

I think the biggest rabbit hole of this kata is that clock times are modular by nature. After 24:00 you start again at 00:01 and so on. Simply put, it cycles.

So you need to account for those cases in which there’s a difference between for example, 23 hours and 04 hours, you have to see how many hours until 24 and then add it to the time elapsed on B from 0 to that unless the hour is greater than A and less than or equal to 24.

Examples:

24 -> 24 = 0 // May even be 24 depending on how you view
// things; this is kind of an especial case
23 -> 24 = 1
10 -> 20 = 10
10 -> 9 = 14 + 9 = 23

There’s a pattern here that you need to notice in order to solve it but you also need to take in consideration the minutes, so you need to multiply the hours by 60 and then add the minutes. 24 hours are equivalent to 1440 minutes; just for reference.

Functions you may need:

  • parseInt
  • String.prototype.split
  • Math.abs

After looking at the kata again, I really don’t understand how it’s coming to the maximum time interval. Logically just looking at the time that the alarm goes off, the first 2 test cases makes sense, but then you look at the third one, and it shows that 9 hours and 10 minutes is the longest interval that the alarm did not go off…

Which makes no sense to me, unless I’m looking at it wrong, as if you look in sequential with the time stamp of the time in the array, the longest time frame without it going off is 8 hours and 26 minutes. So I have no idea, if the wording is bad on the test case, or I’m just dumb lol.

Solved it; there’s 2 caveats:

You have to take in consideration the minute it takes to go off, so you add 1.
Also, the times are not sorted by default, you have to sort it yourself in ascending order
You have to add the first item of the list at the end of the list because of the following edge case: the interval between the last time and the first time of the list.

Spoilers of my solution with RamdaJS:

Welp i did what you said and I passed 0 tests out of 202. code :

function solve(arr){ 
  for (let i = 0; i < arr.length; i++) { 
   arr[i] = arr[i].replace(/:/g,".");
  }
  console.log(arr)
let array = []

for ( let i = 0; i < arr.length; i++) { 
  if (i % 2 === 0) { 
    array.push(Number(arr[i]) - Number(arr[i+1])-0.1)
  }
}

console.log(array)
let endArr = [];
for (let num of array) { 
  let integer = Math.floor(num)
  let float = (num%1).toFixed(2)
  
  if (float > 0.60) { 
    float-=0.40;
  }
  let newNum = integer + float;
  endArr.push(newNum)
}

return endArr.sort((a,b)=> b-a)[0].toString()
}

solve(["23:00","04:22","18:05","06:24"])

your code don’t pass on codewars?
edit: also i dont understand ur code at all…

anybody else interested in helping to solve the problem, feel free to lay down your tips and tricks too. i been on the fence of this problem for over 15 hours

1 Like

what are you trying to do?

I see these steps here:

  • convert everything to minutes (split the time, and then hours*60+mins)
  • sort ascending
  • calculate intervals (considering that last item of the array is to be checked with first item of the next day, so adding 24h to it)
  • Math.max() and convert back to time

Of course it doesn’t pass, the library Ramda does not exist in Codewards.

You have to come up with the code to do all those things yourself:

1- How to create intervals:

// go from this
[1,2,3,4]
// to this
[[1,2],[2,3],[3,4]]
// and then to this
[[1,2],[2,3],[3,4],[4,1]]

2- How to convert a string like "HH:MM" to a tuple of [23, 55] for example. And then to the equivalent in total minutes.

3- How to convert an amount of minutes back to the string "HH:MM".

4- How to get the maximum value of of an array, I’ll give you this wisdom for free:

function maximum(list) {
  return Math.max.apply(null, list);
}

5- How to sort an array in ascending order

6- How to get rid of duplicated elements


Now, the core function that ultimately makes the whole challenge take form is:

function diff([minutesA, minutesB]) {
  return minutesB <= minutesA
    ? (1440 - minutesA) + minutesB - 1
    : minutesB - minutesA - 1
}

Remember that 1440 is the equivalent of 24 hours in minutes. This function computes how many minutes apart are A from B. But for this, you have to make sure that the number of minutes from 0:00 up to A or B meet the condition minutesB <= minutesA because remember than subtracting a lower value minus a greater value will yield a negative number and that after 24 hours the number starts from 1 again. The -1 is because the challenge says the alarm goes off for a full minute.

How do you split the time? I been told my way of using split is incorrect.

you were splitting each character, but you can split the time between hours and minutes so you get an array like ["hh", "mm"] and it easier to make any calculation
you just need to use the right argument for split

This is how you do it:
convert both times into minutes. 18hr60min/hr + 5 - 6hr60min/hr + 24 = 701min

then convert 701min into hours and mins:
701 % 60 = 11 hrs
701 // 60 = 41 syntax assumed here is for python.

Time will 11:41 hrs