Code Golf Asked by user92069 on October 27, 2021
Let’s begin with a thought experiment. You have a clock and a timer, in which you start the timer when the clock shows exactly hh:mm.00
.
hh
is 0<=h<23
. (Because 23
inputs are unsolvable, you aren’t required to handle that)00.00
. The number to the right of .
isn’t milliseconds; it’s seconds.What you need to do is to find out when the two numbers represented by clock time (hh:mm) is respectively equal to the timer time (mm.ss); e.g. 13:24 is "respectively equal" to 13.24. There can potentially be more than one time.
Say the input is 1:59
.
Clock: 1:59
Timer: 0.00 (The timer just started)
...
Clock: 1:59
Timer: 0.59 (59 seconds later...)
...
Clock: 2:00
Timer: 1.00 (As the timer's second section rounds up to the minute section, the clock time gets incremented by a minute. And the 59 minutes in the clock section gets rounded up to the hour section, hence the 2:00.)
...
Clock: 2:00
Timer: 1.59 (59 seconds later...)
...
Clock: 2:01
Timer: 2.00 (The timer minute gets rounded up, as the clock time increments by a minute)
...
Clock: 2:01
Timer: 2.01 (Now the clock time is "respectively equal" to the timer time)
Therefore you need to output 2:01
for the 1:59
input.
Here is a sample program I use to check my test cases.
0:59 -> 0:59 (or 1:00, if your answer supports that)
1:30 -> 1:31
2:59 -> 3:02
1:59 -> 2:01
3:58 -> 4:02
22:01->22:23
hh:mm
, you can nevertheless take input in a list, e.g. [hh,mm]
, or any format suitable for your answer.[mm,ss]
.23
.Based on Jo King's Raku answer. Input and output is in the form of 2-vectors.
<.@(1.01694&*)&.(60&#.)
Explanation:
&.(60&#.) NB. Under base 60,
(1.01694&*) NB. multiply by the magic number,
<.@ NB. then floor the result.
Answered by Silvio Mayolo on October 27, 2021
h=>m=>[h+=h+m>59,(h+m)%60]
Based off of SomoKRoceS' JavaScript answer
Input: two integers, output: List of [mm,ss]
Answered by Thomas on October 27, 2021
Port of Jo's Raku solution
ì60'*1.0#©4
This might work for 10 but I haven't fully tested it:
ì60'*6/5.9
ì60'*1.0#©4 :Implicit input of integer array
ì60 :Convert from base-60
'* :Multiply by
1.0#©4 : 1.01694
:Implicitly convert back to base-60 and output
Answered by Shaggy on October 27, 2021
h=>m=>[~~(h+(i=h+m)/60)%60,~~(61*i/60)%60]
Port of the approach used in @ovs' Python answer.
Input: two integers, output: List of [mm,ss]
Answered by SomoKRoceS on October 27, 2021
d+
$*
^
$'
+`:1{60}
1:
(1*):1*:
$.1:$1
:(1{60})?(1{10})*(1*)
:$#2$.3
Try it online! Link includes test cases. Uses @ovs' formula. Explanation:
d+
$*
Convert to unary.
^
$'
Duplicate the input, which effectively sums the two values together.
+`:1{60}
1:
Add a sixtieth of the sum to the first value.
(1*):1*:
$.1:$1
Delete the remainder of the sum, convert the first value to decimal, and also add it to the second value (still in unary).
:(1{60})?(1{10})*(1*)
:$#2$.3
Convert the second value modulo sixty to two decimal digits.
Answered by Neil on October 27, 2021
ηO¤60÷+60%
Port of the approach used in @ovs' Python answer.
I/O as a pair of integers.
Try it online or verify all test cases.
Explanation:
η # Get the prefixes of the (implicit) input-pair: [h,m] → [[h],[h,m]]
O # Take the sum of each inner list: [h,h+m]
¤ # Push the last item (without popping): h+m
60÷ # Integer-divide it by 60: (h+m)//60
+ # Add it to both values: [h+(h+m)//60,(h+m)+(h+m)//60]
60% # Take modulo-60 on both: [(h+(h+m)//60)%60,((h+m)+(h+m)//60)%60]
# (after which the result is output implicitly)
Answered by Kevin Cruijssen on October 27, 2021
+α_╙╟/+╟%
Port of the approach used in @ovs' Python answer.
Input as two loose integers. Output as a pair.
Explanation:
+ # Add the two (implicit) inputs together: h+m
α # Wrap two items in a list, which will use the (implicit) first input: [h,h+m]
_╙ # Duplicate it, and pop and push the maximum: h+m
╟/ # Integer-divide it by 60: (h+m)//60
+ # Add it to both values in the list: [h+(h+m)//60,h+m+(h+m)//60]
╟% # Take modulo-60 on both: [(h+(h+m)//60)%60,(h+m+(h+m)//60)%60]
# (after which the entire stack joined together is output implicitly as result)
Answered by Kevin Cruijssen on October 27, 2021
function(h,m)c(t<-(h*60+m)/59,60*t%%1)%/%1
Version 2: -3 bytes thanks to clarification that we don't need to output the first match when the timer & clock show the same numbers.
So this version outputs the second match for an input of 0:59
(in other words, 1:00
instead of the first match at 0:59
), and similarly for all other outputs that can end with :59
or :00
.
function(h,m)c(t<-(h+m/60)*6/5.9,60*t%%1)%/%1
Outputs the first timer-clock match (so, always a match ending 0:59
instead of a subsequent match ending :00
).
This exploits the floating-point rounding of *6/5.9
to slightly less than *60/59
, but using the same number of characters. This effectively gives us a floor-like result that rounds-down exact integers in the output (the desired behaviour). Using *60/59
gives an exact floating-point result and so doesn't do this.
(Frustratingly, though, it still isn't as short as simply porting ovs's approach for 43 bytes). Version 2 (above) is shorter.
Answered by Dominic van Essen on October 27, 2021
0+|*/.98334
Since we know the bounds of the input, we can substitute a constant operation and a floor on the input in base 60. That number by the way is around 1358/1381
, which is the maximum the input differs from the output in base 60. There may be a smaller constant, or at least a smaller way to represent it. For reference, the shortest constant you can multiply by, rather than divide, is 1.01694
.
Answered by Jo King on October 27, 2021
Answered by ovs on October 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP