5
\$\begingroup\$

Inputs:

  • current hour: from 0 to 23 (inclusive)
  • delta of hours: [current hour] + [delta of hours] = withing the range of Integer

Output:

  • delta of days: the value which shows difference in days between day of current hour and day of sum [current hour] + [delta of hours]

Scoring

  • Answers will be scored in bytes with fewer bytes being better.

Examples:

  • [current hour] = 23; [delta of hours] = -23; delta of days = 0
  • [current hour] = 23; [delta of hours] = 1; delta of days = 1
  • [current hour] = 23; [delta of hours] = 24; delta of days = 1
  • [current hour] = 23; [delta of hours] = 25; delta of days = 2
  • [current hour] = 23; [delta of hours] = -24; delta of days = -1
  • [current hour] = 23; [delta of hours] = -47; delta of days = -1
\$\endgroup\$
  • \$\begingroup\$ Welcome to CGCC! The code-challenge tag is reserved for custom winning criteria. Did you mean code-golf? \$\endgroup\$ – Arnauld Jul 26 at 9:39
  • \$\begingroup\$ @Arnauld , yeah, is it enough to specify tag? \$\endgroup\$ – Ivan Gerasimenko Jul 26 at 9:49
  • \$\begingroup\$ Yes, that's fine. \$\endgroup\$ – Arnauld Jul 26 at 9:51
  • 1
    \$\begingroup\$ Welcome to the site! I am going to have to disagree with Arnauld and suggest that a description of the winning criterion is present in the body of the challenge. Here is a good summary of why I think this which seems to have the approval of our community. I've gone ahead and edited a winning criterion into your challenge based on my best guess feel free to change it. \$\endgroup\$ – Sriotchilism O'Zaic Jul 26 at 11:38
  • \$\begingroup\$ @SriotchilismO'Zaic , nice! Thank you \$\endgroup\$ – Ivan Gerasimenko Jul 26 at 11:47

19 Answers 19

7
\$\begingroup\$

JavaScript (ES6),  26  25 bytes

Takes input as (current)(delta).

a=>b=>(a+b-(a<-b)*2)/3>>3

Try it online!

Commented

a =>                // a = current hour
b =>                // b = delta of hours
  (                 //
    a + b           // 1) we compute the sum
    - (a < -b) * 2  //    and we subtract 2 if this sum is negative
                    //    examples:
                    //      51 remains 51
                    //      23 remains 23
                    //      -1 is turned into -3
  ) / 3             // 2) float division by 3
                    //    examples:
                    //      51 is turned into 17
                    //      23 is turned into 7.666…
                    //      -3 is turned back into -1
  >> 3              // 3) right arithmetic shift by 3 on the integer part
                    //    examples:
                    //      17 becomes 2
                    //      7.666… becomes 0
                    //      -1 is unchanged

JavaScript (ES6), 26 bytes

More straightforward, but less fun and 1 byte longer anyway.

Takes input as (current)(delta).

a=>b=>Math.floor((a+b)/24)

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Jelly, 4 bytes

+:24

Try it online!

+ add the arguments

:24 integer divide by 24

\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog Unicode), 6 bytesSBCS

Anonymous tacit infix function, taking current and delta as arguments.

⌊24÷⍨+

Try it online!

+ add the arguments

24÷⍨ divide that by 24

 floor

\$\endgroup\$
4
\$\begingroup\$

05AB1E (legacy), 4 bytes

+24÷

Try it online or verify all test cases.

05AB1E, 11 5 bytes

+24/ï

-6 bytes by porting @Adam's approach in his APL answer.

Try it online or verify all test cases.

Explanation:

+      # Sum the two (implicit) input-integers together
 24÷   # Integer-divide this sum by 24
       # (after which the result is output implicitly)

+      # Sum the two (implicit) input-integers together
 24/   # Divide this sum by 24
    ï  # Floor
       # (after which the result is output implicitly)

The legacy version of 05AB1E uses a Python compiler. When integer-dividing, it will always floor the integer, whether the integer it divides is positive or negative.

The new version of 05AB1E uses an Elixir compiler. When integer-dividing in the new version of 05AB1E, it will round towards 0 (so basically truncates the decimal digits after dividing). So this will floor for positive integers, but ceil for negative integers.

\$\endgroup\$
  • 1
    \$\begingroup\$ 5 bytes? \$\endgroup\$ – Expired Data Jul 26 at 10:41
  • \$\begingroup\$ @ExpiredData Ah, noticed your comment after Adam's answer. But you're indeed right this can be done much easier.. Maybe I should just delete my answer out of shame and let you get the 5-byter. ;) \$\endgroup\$ – Kevin Cruijssen Jul 26 at 10:52
  • \$\begingroup\$ nah I'm sure you'll pay me back in bytes saved given how terrible me 05AB1E is at the moment so don't worry! \$\endgroup\$ – Expired Data Jul 26 at 10:53
  • \$\begingroup\$ @Adám That's what I initially had in mind when I saw the challenge, but it doesn't work for the [23,-24] and [23,-48] test cases. Which is why I had that weird 11 byter as my initial answer. Integer division will ceil for negative integers, instead of floor. \$\endgroup\$ – Kevin Cruijssen Jul 26 at 11:29
  • \$\begingroup\$ Ah, so it rounds towards 0. Fair enough. \$\endgroup\$ – Adám Jul 26 at 11:32
4
\$\begingroup\$

Python 3, 20 19 bytes

Simple lambda:

lambda*a:sum(a)//24

-1 byte thanx to Jonathan Allan

Try it online!

Full program for 38 bytes:

print((int(input())+int(input()))//24)
\$\endgroup\$
  • 1
    \$\begingroup\$ Save a byte with *args like so: lambda*a:sum(a)//24. Switch to Python 2 and save another with lambda*a:sum(a)/24. \$\endgroup\$ – Jonathan Allan Jul 26 at 15:18
3
\$\begingroup\$

R, 28 16 bytes

Just a port of most of the other answers...
-12 bytes thanks Giuseppe

sum(scan())%/%24

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Use x%/%y instead of floor(x/y) \$\endgroup\$ – Giuseppe Jul 26 at 20:16
  • 1
    \$\begingroup\$ Or better yet, take input from stdin and do sum(scan())%/%24 \$\endgroup\$ – Giuseppe Jul 26 at 20:18
  • \$\begingroup\$ Nice one. Will have to remember that trick. \$\endgroup\$ – Robert S. Jul 26 at 20:21
3
\$\begingroup\$

Haskell, 18 16 bytes

((`div`24).).(+)

Try it online!

If taking input as a list is allowed, this can be done in 13 bytes (thanks cole):

(`div`24).sum

Try it online!


I've seen people leave off the function declaration in the byte count for pointfree style, so I'm doing the same here.

Explanation:

(`div`24)

div is the shorter integer division operator. Haskell has this nice syntactic sugar where you can curry infixes by putting the argument on its respective side of the infix (thanks mimi), so this is a function that does integer division by 24.

The double composition is a little weird to explain, so I'll follow the template of this great SO answer:

f a b = (`div`24) (a + b)       -- what we want
f a b = (`div`24) ((+) a b)     -- un-infixing (+)
f a b = ((`div`24) . ((+) a)) b -- definition of function composition (partially applying (+) onto a)
f a = ((`div`24) . ((+) a)       -- pointfree reduction
f a = ((`div`24) .) ((+) a)     -- associative property
f a = (((`div`24) .) . (+)) a   -- definition of function composition
f = ((`div`24).) . (+)            -- pointfree reduction

When this takes a list as input, the sum function only has one argument, so the extra composition isn't needed (since we don't have to do that extra curry).

\$\endgroup\$
  • \$\begingroup\$ You can use backquotes instead of flip: ((`div`24).).(+). \$\endgroup\$ – nimi Jul 27 at 11:09
  • 1
    \$\begingroup\$ if you can take input as an array, then (`div`24).sum is shorter \$\endgroup\$ – cole Jul 28 at 6:04
2
\$\begingroup\$

Ohm v2, 4 bytes

+24v

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 26 bytes

a=>b=>Math.Floor((a+b)/24)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 11 bytes

(*+*)div 24

Try it online!

Anonymous Whatever lambda that takes two arguments and returns the sum of the two integer divided by 24

\$\endgroup\$
1
\$\begingroup\$

Lua, 32 26 bytes

print((arg[1]+arg[2])//24)

Try it online!

Rather boring solution. Take input as arguments, print to stdout. Require Lua 5.3 or greater.

Old solution: Lua, 32 bytes

function(a,b)return(a+b)//24 end

Try it online!

Same idea, but implemented as function instead of full program.

\$\endgroup\$
  • \$\begingroup\$ Nice. Thought briefly about a function, but didn't take the time. \$\endgroup\$ – ouflak Jul 29 at 20:45
  • \$\begingroup\$ @ouflak New solution is full program taking input as command like arguments tho. \$\endgroup\$ – val Jul 29 at 20:56
1
\$\begingroup\$

Lua, 70 50 bytes

c,d=io.read():match("(%S+) (%S+)")print((c+d)//24)

-20 bytes thanks to val

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Second line can be made shorter by specifying valid input format. Third line is just print((c+d)//24) in Lua 5.3. There are also some other ways to golf this. \$\endgroup\$ – val Jul 28 at 9:36
  • \$\begingroup\$ Mind if I'll edit and improve your answer later today? Mine is shorter, but your is full program. \$\endgroup\$ – val Jul 28 at 9:37
  • \$\begingroup\$ @val, Just go ahead and post your solution with your own TIO. Or if you don't want to do that, just comment here your suggested improvement and I'll edit it giving credit. These options seem to be the way things work around here. \$\endgroup\$ – ouflak Jul 28 at 21:46
  • \$\begingroup\$ 50 bytes: Try it online!. Use space separated input. I'll also add another solution which take arguments from command line to my answer. \$\endgroup\$ – val Jul 29 at 14:25
0
\$\begingroup\$

I, 6 bytes

Anonymous tacit infix function, taking current and delta as arguments.

+/24.m

Try it online!

Beginning with:

+ the sum of the arguments

/ divide by:

24 twentyfour

. apply:

m floor (minimum)

\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 41 bytes

\d+,?
$*
(1+)-\1
-
^((-)1|1{24})*.*
$2$#1

Try it online! Link includes test suite. Explanation:

\d+,?
$*

Convert both inputs to unary and take the sum if the second input is positive.

(1+)-\1
-

But if the second input is negative then take the difference.

^((-)1|1{24})*.*
$2$#1

Floor divide the input by 24 and convert to decimal. For positive numbers the first alternation never matches so this just counts the number of whole multiples of 24 in the number. For negative numbers the leading -1 counts as an extra negative multiple of 24 in addition to any remaining multiples of 24, thus achieving the desired floor division.

\$\endgroup\$
0
\$\begingroup\$

Python 2, 18 bytes

lambda*a:sum(a)/24

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5 (-MPOSIX=floor -MList::Util=sum -alp), 19 bytes

$_=floor+(sum@F)/24

TIO

\$\endgroup\$
0
\$\begingroup\$

Whitespace, 49 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][T  S S S _Add][S S S T T   S S S N
_Push_24][T S T S _integer_division][T  N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs, and new-lines only).

Explanation in pseudo-code:

Integer a = STDIN as integer
Integer b = STDIN as integer
a = a + b
a = a integer-divided by 24
Print a as integer to STDOUT
\$\endgroup\$
0
\$\begingroup\$

Forth (gforth), 12 bytes

: f + 24 / ;

Try it online!

Code Explanation

: f           \ start a new word definition
  +           \ add current hour and delta
  24 /        \ (integer) divide result by 24, round towards negative infinity
;             \ end word definition
\$\endgroup\$
0
\$\begingroup\$

PHP, 29 bytes

<?=floor((date(G)+$argn)/24);

Try it online!

Full program, with current hour being the current hour on the platform host, and delta of hours input via STDIN.

Or

PHP, 30 bytes

<?=floor(array_sum($argv)/24);

Try it online!

Input of current hour and delta of hours via command line.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.