# Introduction

A bell tower will ring its bells every hour, n times, with n being the the current hour on a 12 hour clock.

For example, a bell will ring 5 times at 5pm, and 10 times at 10am.

Given two times in a suitable format, output the number of times the bell will ring, inclusive of the start and end times

# Examples

"10am-12pm"
10+11+12= 33

[01:00, 05:00]
1+2+3+4+5 = 15

[11, 15]
11+12+1+2+3 = 29

[10:00pm, 10:00am]
10+11+12+1+2+3+4+5+6+7+8+9+10 = 88

If the start is the same as the end then the you simply just ouput the number of chimes for that hour:

[5pm, 5pm]
5 = 5

As you can see, you may choose an input method but the output must be an integer on its own (or an acceptable alternative) trailing/ leading newlines and spaces are allowed.

Note:

• inputs may span from the afternoon of one day to the morning of the next.
• the difference between the two times will never be more than 24 hours.
• input is flexible so long as you clearly state what format your input is.
• your input must have a clear distinction between AM and PM.
• Do we choose our own input method, or does it have to support all the mentioned? – anonymous2 Oct 3 '16 at 16:10
• You may choose the input method – Shaun Wild Oct 3 '16 at 16:13
• You should make it more clear that the inputs can go from pm to am, thus crossing to a 2nd day. – mbomb007 Oct 3 '16 at 16:19
• Will midnight be given as 0 or 24? – xnor Oct 3 '16 at 17:21
• We encourage the use of the Sandbox to work out any issues with challenges before they are posted to the main site. – user45941 Oct 3 '16 at 17:44

# JavaScript (ES6), 38 35 bytes

f=(x,y)=>~-x%12-~(x-y&&f(x%24+1,y))

Recursively adds the current number of bell rings to the total. Called like f(11,15); midnight is represented as 24. I got part of the ~- trick from @xnor's Python answer.

## Test snippet

f=(x,y)=>~-x%12-~(x-y&&f(x%24+1,y))
g=(x,y)=>console.log("x:",x,"y:",y,"result:",f(x,y))

g(10,12)
g(1,5)
g(11,15)
g(22,10)
<input id=A type="number" min=1 max=24 value=9>
<input id=B type="number" min=1 max=24 value=17>
<button onclick="g(A.value,B.value)">Run</button>

### Non-recursive version (Firefox 30+), 56 bytes

(x,y,t=0)=>[for(_ of Array((y-x+25)%24))t+=x++%12||12]|t

Equivalent to the following ES6 function:

(x,y,t=0)=>[...Array((y-x+25)%24))].map(_=>t+=x++%12||12)|t

# Python 2, 46 bytes

f=lambda x,y:(x%12or 12)+(x-y and f(-~x%24,y))

Based on my JS answer. The recursive formula f for the solution is defined as so:

2. Take x mod 12; if this is 0, take 12 instead.
3. If x != y, add the result of f(x+1 mod 24, y).

# Python 2, 59 54 bytes

a=lambda x,y:sum(1+i%12for i in range(x-1,y+24*(x>y)))
Equivalent to
summ=0
if start > end:
end+=24
for hour in range(start-1,end):
summ +=1+hour%12
print summ
• I think you don't need the a= part. – acrolith Oct 3 '16 at 16:50
• @daHugLenny it need to be a full(usable) function – Rod Oct 3 '16 at 17:01
• (y+24)%24 is just y – Vladimir Cravero Oct 3 '16 at 17:24
• @Rod You don't need a=. It's allowed to be a pure lambda. – Yytsi Oct 4 '16 at 11:06
• @VladimirCravero Of course not. It is the same as y%24. – Erik the Outgolfer Oct 4 '16 at 11:11

# 05AB1E, 13 bytes

With a lot of help from Emigna.

-24%Ý¹+<12%>O

Uses the CP-1252 encoding. Try it online!

## Python, 42 bytes

f=lambda a,b:~-a%12-~(b-a and f(-~a%24,b))

A recursive function that takes two numbers from 0 to 23. Expanding the ~x's to -x-1 gives

f=lambda a,b:(a-1)%12+1+(b-a and f((a+1)%24,b))

The expression (a+1)%12+1 converts a time to the number of rings 1 to 12. Then, the lower bound is incremented modulo 24 and the function for the recursive result is added. That is, unless the current hour is the end hour, in which case we stop.

I've been trying to write a purely arithmetical solution instead, but so far I've only found long and messy expressions.

• Ah, I get it: it's basically the same technique as my Python answer, but with a really clever way of getting around the or. Nice one! – ETHproductions Oct 3 '16 at 17:30

s%e=sum[mod x 12+1|x<-[s-1..e+23],x<e||s>e]

Usage is startHour % endHour, with both inputs given in 24-hr format.

edit: added @xnor's improvement, saving 5 bytes

• Instead of changing e when e<s, you could filter the range s%e=sum[mod(x-1)12+1|x<-[s..e+24],x<=e||s>e]. It then saves a byte to shift x down by 1: s%e=sum[mod x 12+1|x<-[s-1..e+23],x<e||s>e]. – xnor Oct 3 '16 at 21:58

# C#, 73 bytes

a=>b=>{int x=0;for(;;){x+=(a%=24)>12?a-12:a<1?12:a;if(a++==b)return x;}};

Acceptable input: integers in range [0,23].

This solution does not use LINQ.

Full program with test cases:

using System;

namespace HowManyTimesABellTowerRings
{
class Program
{
static void Main(string[] args)
{
Func<int,Func<int,int>>f= a=>b=>{int x=0;for(;;){x+=(a%=24)>12?a-12:a<1?12:a;if(a++==b)return x;}};

Console.WriteLine(f(10)(12));   //33
Console.WriteLine(f(1)(5));     //15
Console.WriteLine(f(11)(15));   //29
Console.WriteLine(f(22)(10));   //88
Console.WriteLine(f(10)(10));   //10
Console.WriteLine(f(11)(10));   //156
Console.WriteLine(f(0)(23));    //156
Console.WriteLine(f(22)(1));    //34
}
}
}

# Jelly, 17 16 15 14 bytes

>×24+⁹⁸r’%12‘S

TryItOnline

How?

>×24+⁹⁸r’%12‘S - Main link: a, b (24 hr integers, midnight may be 0 or 24)
>              - a>b? (1 if true, 0 if false)
×24           - times 24 (24 if a>b, else 0)
+⁹         - add to b (b+24 if a>b, else b)
⁸        - a
r       - range(a, b+24 or b) ([a,a+1,...,b+24 or b])
’      - decrement (vectorises) ([a-1,a,...,b+23 or b-1])
%12   - mod 12 (vectorises) (number of tolls at each occurrence - 1)
‘  - increment (vectorises) (number of tolls at each occurrence)
S - sum

# MATL, 14 bytes

yy>24*+&:12X\s

Input format is as in the third example in the challenge, that is, two numbers in 24-hour format.

Try it online!

### Explanation

Take inputs 22, 10 as an example.

yy      % Take two inputs implicitly. Duplicate both
%   STACK: 22, 10, 22, 10
>       % Is the first greater than the second?
%   STACK: 22, 10, 1
24*     % Multiply by 24
%   STACK: 22, 10, 24
%   STACK: 22, 34
&:      % Binary range
%   STACK: [22 23 24 25 26 27 28 29 30 31 32 33 34]
12X\    % Modulo 12, 1-based
%   STACK: [10 11 12 1 2 3 4 5 6 7 8 9 10]
s       % Sum of array
%   STACK: 88
% Implicitly display

# PHP, 90 Bytes

Input format '[1,24]' between 1 and 24

In this challenge that I am hate why PHP loose against other languages. I prefer to show all my ideas. Maybe an other PHP Crack finds a shorter solution.

<?list($f,$g)=$_GET[b];for($i=$f;$i-1!=$g|$f>$g&!$c;$s+=$i++%12?:12)$i<25?:$c=$i=1;echo$s;

99 Bytes

<?for($i=($b=$_GET[b])[0],$c=($d=$b[1]-$b[0])<0?25+$d:$d+1;$c--;$s+=$i++%12?:12)$i<25?:$i=1;echo$s; 113 Bytes a way with min and max <?for($i=min($b=$_GET[b]);$i<=$m=max($b);)$s+=$i++%12?:12;echo($b[0]>$b[1])?156-$s+($m%12?:12)+($b[1]%12?:12):$s; okay this crazy idea works with an array 149 Bytes fills the array$y[0] and $y[1] if$_GET["b"][0]<=$_GET["b"][1] if$y[1] is null we can sum this array array_diff_key($y[0],array_slice($y[0],$b[1],$b[0]-$b[1]-1,1)) <?for(;++$i<25;)$y[$i>=($b=$_GET[b])[0]&$i<=$b[1]][$i]=$i%12?:12;echo array_sum($y[1]??array_diff_key($y[0],array_slice($y[0],$b[1],$b[0]-$b[1]-1,1)));

This could be golfed down 124 Bytes

<?for(;++$i<25;)$x[($v=($b=$_GET[b])[0]>$b[1])?$i<$b[0]&$i>$b[1]:$i>=$b[0]&$i<=$b[1]][$i]=$i%12?:12;echo array_sum($x[!$v]);

Now at this point we can reduce the array with only two ints 101 Bytes Make 2 sums $x[0] and$x[1]

list($f,$g)=$_GET[b]; if$v=($f>$g then add value to $x[$i<$f&$i>$g] else add value to$x[$i>=$f&$i<=$g] the output will be find by case echo$x[!$v];

<?list($f,$g)=$_GET[b];for(;++$i<25;)$x[($v=$f>$g)?$i<$f&$i>$g:$i>=$f&$i<=$g]+=$i%12?:12;echo$x[!$v]; After that i found a way to calculate the result directly 112 Bytes <?list($x,$y)=$_GET[t];echo(($b=$x>$y)+(($x-($s=$x%12?:12)^$y-($t=$y%12?:12))xor$b))*78-($s*($s-1)-$t*($t+1))/2;

recursive 103 Bytes

<?list($x,$y)=$_GET[t];function f($x,$y){return($x%12?:12)+($x-$y?f(++$x<25?$x:1,$y):0);}echo f($x,$y); # PHP, 69 bytes list(,$i,$a)=$argv;for($a+=$i>$a?24:0;$i<=$a;)$n+=$i++%12?:12;echo$n;

The list extraction was inspired by Jörg Hülsermann's answer but the rest of the similarities are a result of convergent evolution and because it's quite a lot shorter and the conditionals in the loop are different enough I'm posting it as a separate answer.

Takes input as 24 hour times (fine with either 0 or 24). Run like:

# Java 7, 64 bytes

int c(int x,int y){return(x%12<1?12:x%12)+(x!=y?c(-~x%24,y):0);}

Recursive method based on @ETHproductions's Python 2 answer. Uses a 24-hour clock input.

Ungolfed & test code:

Try it here.

class M{
static int c(int x, int y){
return (x%12 < 1
? 12
: x%12)
+ (x != y
? c(-~x % 24, y)
: 0);
}

public static void main(String[] a){
System.out.println(c(10, 12));
System.out.println(c(1, 5));
System.out.println(c(11, 15));
System.out.println(c(10, 22));
System.out.println(c(5, 5));
}
}

Output:

33
15
29
88
5

## Batch, 168 91 bytes

@cmd/cset/ax=(%1+23)%%24,y=x+(%2+24-%1)%%24,z=y%%12+1,(y/12-x/12)*78+z*-~z/2-(x%%=12)*-~x/2

Edit: Saved 77 byte by switching to a closed form for the answer.

• %1 and %2 are the two command-line parameters
• @ Disable Batch's default which is to echo the command
• cmd/c Fool Batch into immediately printing the result of the calculation
• set/a Perform a numeric calculation
• x=(%1+23)%%24, Normalise the starting hour to be the number of hours since 1AM (1PM would also work, but 11 is no shorter than 23)
• y=x+(%2+24-%1)%%24, Normalise the ending hour to be ahead of the starting hour, advancing to the next day if necessary
• z=y%%12+1, Number of bells struck at the ending hour
• (y/12-x/12)*78+ Number of bells due to extra half days
• z*~-z/2- Number of bells from 1 o'clock to the ending hour inclusive
• (x%%=12) One less than the number of bells struck at the starting hour
• *-~x/2 Number of bells that would have been struck from 1 o'clock to the starting hour, but not including the starting hour

### C, 56 Bytes

f(a,b,c=0){while(b-->a){c+=b>12?b-12:b;}printf("%d",c);}

# ><>, 48 + 2 = 50 bytes

<v$&%$-&:$+}:*2c {>:?!v1-}:1+ v?=1l<++1%c+b$
>n;

Input is expected to be present on the stack at program start, so +2 bytes for the -v flag. Input is two integers specifying the hour on the 24 hour clock, so 10am - 10pm would be given as 10 22.

Try it online!

• @LuisMendo Thanks, it's fixed now – Sok Oct 5 '16 at 7:38

# Cubix, 45 44 bytes

Saved 1 byte, thanks to @ETHproductions

My first foray into Cubix...

)$424tU4OI0Iuq;;-!^;^%&21u+rr;ss!;sqU>&%r$@;

Or, cubified:

) $4 2 4 t U 4 O I 0 I u q ; ; - ! ^ ; ^ % & 2 1 u + r r ; s s ! ; s q U > & % r$ @ ; .
. . .
. . .
. . .

You can try it out at the online interpreter. Input is in 24 hour format, with the end time first. For example, from 5pm to 1am the input should be 1 17.

Previous version, 45 bytes:

)$442t\/OI0Iuq;;-!^;^%&21u+rr;ss!;sqU>&%r$@.;
• Thank you for using my language, and great job :-) I see one small byte you can save by rearranging slightly and dropping the no-op: )$424tU4OI0Iuq;;-!^;^%&21u+rr;ss!;sqU>&%r$@; – ETHproductions Oct 29 '16 at 2:07

# Qbasic, 112 bytes

input "",a
input "",b
do
if a=25 then a=1
if a<=12 then
c=c+a
else
c=c+a-12
endif
a=a+1
loop until a=b+1
print c
• Shouldn't you output 12 when both starting and ending hour are zero? – Neil Oct 4 '16 at 12:42

# Python, 73 bytes

It would be so much shorter if we didn't have to support pm to am. I use recursion to support it.

f=lambda a,b:sum([~-i%12+1for i in range(a,b+1)]*(a<b)or[f(a,24),f(1,b)])

Try it online

Without supporting pm to am (45 bytes):

lambda a,b:sum(~-i%12+1for i in range(a,b+1))