Skip to main content
Commonmark migration
Source Link

#JavaScript (ES6), 73 56 54 52 50 bytes

JavaScript (ES6), 73 56 54 52 50 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) 

Try it

##Try it EnterEnter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


Explanation

##Explanation (To be updated shortly.)

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

#JavaScript (ES6), 73 56 54 52 50 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) 

##Try it Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


##Explanation (To be updated shortly.)

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

JavaScript (ES6), 73 56 54 52 50 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) 

Try it

Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


Explanation

(To be updated shortly.)

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

added 158 characters in body
Source Link
Shaggy
  • 45k
  • 4
  • 39
  • 95

#JavaScript (ES6), 73 56 54 5252 50 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) 
(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) 

##Try it Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,(h-g+24*(h<gg+24)+m-l+60*%24+(m<lm-l+60)%60) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


##Explanation (To be updated shortly.)

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

#JavaScript (ES6), 73 56 54 52 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) 

##Try it Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


##Explanation

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

#JavaScript (ES6), 73 56 54 52 50 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) 

##Try it Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


##Explanation (To be updated shortly.)

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

added 792 characters in body
Source Link
Shaggy
  • 45k
  • 4
  • 39
  • 95

#JavaScript (ES6), 73 56 54 52 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) 

##Try it Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


##Explanation

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

#JavaScript (ES6), 73 56 54 52 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) 

##Explanation

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

#JavaScript (ES6), 73 56 54 52 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

(g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) 

##Try it Enter the times in 24-hour format, with the : separator.

o.innerText=(f= (g,l,h,m)=>Math.min(2+h+m,h-g+24*(h<g)+m-l+60*(m<l)) )(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


##Explanation

(g,l,h,m)=> 

Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.

2+h+m 

First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.

h-g+24*(h<g) 

Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.

+m-l+60*(m<l) 

We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.

Math.min() 

Finally, we get the minimum of the 2 numbers to give us our result.

deleted 6 characters in body
Source Link
Shaggy
  • 45k
  • 4
  • 39
  • 95
Loading
deleted 6 characters in body
Source Link
Shaggy
  • 45k
  • 4
  • 39
  • 95
Loading
Source Link
Shaggy
  • 45k
  • 4
  • 39
  • 95
Loading