Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Consecutive Distance Rating

We'll call the consecutive distance rating of an integer sequence the sum of the distances between consecutive integers. Consider 2 9 3 6 8 1.

2 9 3 6 8 1
<----5---->
<-2->
 <--3-->

\2ドル\$ and \1ドル\$ are consecutive integers, and their distance apart in the sequence is \5ドル\$.
\2ドル\$ and \3ドル\$ are consecutive integers, and their distance apart in the sequence is \2ドル\$.
\9ドル\$ and \8ドル\$ are consecutive integers, and their distance apart in the sequence is \3ドル\$.
The consecutive distance rating is the sum of these distances: \10ドル\$.

Challenge

Given a possibly empty list of positive, unique integers, find its consecutive distance rating.

Format

You must accept a list of integers and output an integer in any reasonable format.

Rules

Test cases

[] -> 0
[33] -> 0
[65 57 78 32 81 19 50 24 85 3 97 43 10 73] -> 0
[1 2] -> 1
[2 1] -> 1
[1 2 3] -> 2
[1 3 2] -> 3
[31 63 53 56 96 62 73 25 54 55 64] -> 26
[54 64 52 39 36 98 32 87 95 12 40 79 41 13 53 35 48 42 33 75] -> 67
[94 66 18 57 58 54 93 53 19 16 55 22 51 8 67 20 17 56 21 59] -> 107

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ The test wrapper is actually by @Kirill ;-) \$\endgroup\$ Commented Jun 15, 2021 at 16:01
  • \$\begingroup\$ Then why not merge our solutions? 45 bytes \$\endgroup\$ Commented Jun 15, 2021 at 16:09

AltStyle によって変換されたページ (->オリジナル) /