# Count count count

Description

You have a list of integers and start counting from the first term to the next and continue from that to the next and so on..
How many times have you counted?

For example given [ 2, 5, 3, 8 ]
you start at 2 and count to 5 -> 3 4 5 (3 times)
then from 5.. 4 3 (2 times)
and finally 4 5 6 7 8 (5 times)
You counted 10 times.

With ragged lists, when you have a sublist just count the sublist and use that result.

for example [ 1, [ 3, 6 ] ]
1 to ( count[ 3, 6 ] ->3 )
1 to 3 -> 2

For empty lists you count 0 times, the same applies for lists of one element or lists of equal elements.
[ ] -> 0
[ 99 ] -> 0
[ 8, 8, 8 ] -> 0

Given a ragged list of non negative integers count it as described above.

Test cases

[] -> 0
[[2,5]] -> 0
 -> 0
[8,8,8] -> 0
[1,1] -> 0
[0,9] -> 9
[2,5,3,8] -> 10
[1,[3,6]] -> 2
[[],4,[[5,3],2]] -> 8
[100,100,[7,[[]]],[[4,8]]] -> 100
[[9,5],0] -> 4
[[],[]] -> 0
[[],3,[]] -> 6
[1,[2,5,10]] -> 7
[2,6,9] -> 7
[[1,2],[1,3],[1,4]] -> 2
[1,99] -> 98
[11,9,6] -> 5
[0,1,2,3,4] -> 4
[2,3,3,3,2] -> 2
[0,1,0,1] -> 3


This is , all usual golfing rules apply.

$<; {(<1*);(1*)(1*),\2(1*)(,|]) 134;245 }<(1*);1*] 1 1  Try it online! Link includes test cases. Explanation: \d+ *  Convert to unary. \[ <;  Place markers at the start of each sublist. { }  Repeat until no more replacements can be made. (<1*);(1*)(1*),\2(1*)(,|]) 134;245  If a sublist starts with two integers, then remove the first and insert the difference before the marker, which tracks the cumulative absolute total. <(1*);1*] 1  When a sublist runs out of integers then simply replace it with its total. 1  Convert to decimal. # Charcoal, 56 bytes ⊞υθＦυＦιＦ⁼κ⁺⟦⟧κ⊞υκＷυ«≔⊟υι≔⟦⟧ηＷι⊞ηΣ⁺⟦⁰⟧⊟ι⊞ι↨ＥΦηλ↔⁻κ§ηλ¹»Ｉθ  Try it online! Link is to verbose version of code. Explanation: ⊞υθＦυ  Start extracting all of the sublists. ＦιＦ⁼κ⁺⟦⟧κ⊞υκ  Loop over all the lists and when a sublist is found then push it to the predefined empty list. Ｗυ«  Process all of the sublists. ≔⊟υι  Get the deepest remaining sublist. ≔⟦⟧η  Start collecting the terms in this sublist. Ｗι⊞ηΣ⁺⟦⁰⟧⊟ι  Remove the terms from the list and push their numeric value to the temporary list. Any sublists will have been reduced to their count as a single term [n] at this point, so prepending  results in [0, n] and taking the sum returns n, while adding  to an integer n results in [n] and then taking the sum returns n again. ⊞ι↨ＥΦηλ↔⁻κ§ηλ¹  Compute the absolute pairwise differences and push the total to the list. »Ｉθ  Output the final total of the original list. # JavaScript, 60 52 bytes f=(a,t=0)=>p=a.map?.(p=x=>t+=Math.abs(p-f(x)|0))?t:a  f=(a,t=0)=>p=a.map?.(p=x=>t+=Math.abs(p-f(x)|0))?t:a console.log(f([])) console.log(f([[2,5]])) console.log(f()) console.log(f([8,8,8])) console.log(f([1,1])) console.log(f([0,9])) console.log(f([2,5,3,8])) console.log(f([1,[3,6]])) Saved 8 bytes thanks to tsh. • f=(a,t=0)=>p=a.map?.(p=x=>t+=Math.abs(p-f(x)|0))?t:a – tsh Jan 27, 2022 at 5:39 # SWI-Prolog, 123 105 bytes []+0. [_]+0. [H,S|T]+C:-(is_list(H)->H+G;G is H),(is_list(S)->S+Q;Q is S),D is abs(Q-G),[S|T]+R,C is D+R.  -18 bytes thanks to Wheat Wizard! Try it online! • Can't f be an infix? Jan 27, 2022 at 9:37 • Try it online! Jan 27, 2022 at 10:00 • @WheatWizard I... never learned about infixes... I should probably read the Prolog tips thread, huh? Thanks! Jan 27, 2022 at 17:50 • Think you can also shorten the base case somewhat. Mar 17, 2022 at 8:43 # Ruby, 75 bytes f=->a{a*0==0?a:([a]*(a.size%2)+a).map(&f).each_cons(2).sum{(_2-_1).abs}} Attempt This Online! # 05AB1E, 21 bytes "Ð˜Êi®δ.V}Ð˜Qi¥ÄO"©.V  Ugh.. :/ Explanation: "..." # Define the recursive string below © # Store it in variable ® (without popping) .V # Evaluate and execute it as 05AB1E code, using the (implicit) # input-list as argument # (after which the result is output implicitly) Ð˜Ê # Check if there is an inner list: Ð # Triplicate the current list ˜ # Flatten it Ê # Check if the top two lists are NOT the same i # If this is truthy: δ # Map over each inner item: ® .V # Do a recursive call } # After the if-statement, whether we executed its body or not: Ð˜Q # Check if this is a 1D list: Ð # Triplicate the current list ˜ # Flatten it Q # Check if the top two lists are equal i # If this is truthy: ¥ # Pop the list, and push its deltas/forward-differences Ä # Convert each to its absolute value O # And then sum this list  # Python NumPy, 69 bytes f=lambda s:sum(abs(s*-1or diff([*map(f,s or[])]))) from numpy import* Attempt This Online! I suspect it's 66 bytes in Python2 but have no way of checking. # Perl 5, 88 bytes sub f{_=pop;1while s!\[[\d,]*$!@a=$&=~/\d+/g;0+sum map abs$a[$_-1]-$a[$_],1..$#a!e;\$_}