g | x | w | all
Bytes Lang Time Link
008Nibbles240717T044325ZConor O&
054Octave240716T180354Zint 21h
122Scala 3231119T000159Z138 Aspe
012Uiua231118T211111Zchunes
nan230217T112014ZThe Thon
054JavaScript V8210619T081326ZJames
055Julia 1.0210616T141817Zmapi1
010Japt mx210615T221034ZShaggy
008Jelly210615T092442Zcaird co
012Japt x210615T135248ZAZTECCO
015Brachylog210616T060529ZUnrelate
009MATL210615T213533ZLuis Men
013Vyxal s210615T093018Zemanresu
042Wolfram Language Mathematica210615T193917Zatt
045R210615T125800Zpajonk
048Ruby210615T141640ZG B
047R210615T152309ZDominic
052Excel210615T142138ZAxuary
048R210615T140118ZKirill L
022J210615T112813ZJonah
077Red210615T103951ZGalen Iv
129Red210615T102219ZRazetime
060JavaScript Node.js210615T101845Zuser1006
060Python 2210615T100046Zxnor
015Charcoal210615T100344ZNeil
00805AB1E210615T095151Zovs
091Python 3210615T094606ZWasif

Nibbles, 17 16 nibbles (8.5 8 bytes)

+.$??@+$~!=$?_@-

Attempt This Online! Corresponds to the 8 bytes 8c 3f f4 83 0c 03 f5 49. -0.5 bytes thanks to @Dominic van Essen, for observing that the if/else ? saves the value used for the conditional as additional context, allowing me to use my longer solution while avoiding the explicit save!

Previous 8.5 byte solution: +.$*!=?@$;?@+$~`$.

Explanation

This is similar in execution to @caird coinheringaahin g's 8-byte Jelly answer.

+.$??@+$~!=$?_@-
+.$??@+$~!=$?_@-$$  # (with implicit argument)
 .$                 # map over each element X in the input:
      +$~           #   compute X+1
    ?@              #     and its index in the original list
   ?                #   if it is positive (aka found):
                    #     then that value is saved in $
         !=         #     so we take the absolute difference of...
           $        #       that saved index
            ?_@     #       and the index of X in the original list
               -$$  #   otherwise, return X-X, aka 0

Old Explanation

+.$*!=?@$;?@+$~`$
+.$*!=?@$;?@+$~`$$  # (with implicit argument)
 .$                 # map over each element X in the input:
    !=              # absolute difference of...
          ?         # ...index of
            +$~     #      X+1
           @        #      in the original list input
         ;          #    (save this index for later)
      ?@$           # ..and the current index (1-based)
   *                # multiplied by
               `$   # the sign of
                 $  # the index of X+1 we saved (which is 0 when not found)
+                   # sum resulting list

Octave, 54 bytes

@(a){[x j]=sort(a)
sum(abs(j(d=diff(x)<2)-j([!1 d])))}

Try it online!

My idea was to sum the absolute values of the differences of ordering indices of the elements while filtering away the lone elements (there is a possible similarity to one R answer, but I haven't check it in details).

Scala 3, 122 bytes

Golfed version. Attempt This Online!

l=>l.flatMap(x=>l.indexOf(x+(if(l.contains(x+1))1 else 0))match{case -1=>None;case i=>Some(Math.abs(l.indexOf(x)-i))}).sum

Ungolfed version. Attempt This Online!

def calculate(list: List[Int]): Int = {
  list
    .flatMap(x =>
      list.indexOf(x + (if (list.contains(x + 1)) 1 else 0)) match {
        case -1 => None
        case i  => Some(Math.abs(list.indexOf(x) - i))
      }
    )
    .sum
}

Uiua, 12 bytes

/+⌵/-⍉⊚=1⊞-.

Try it!

/+⌵/-⍉⊚=1⊞-.
         ⊞-.  # difference table
      ⊚=1     # coordinates of ones
     ⍉        # transpose
  ⌵/-         # absolute difference of columns
/+            # sum

Thunno, \$ 24 \log_{256}(96) \approx \$ 19.75 bytes

2zPgAu-1=keez0AhEeAuADES

Attempt This Online!

Explanation

2zP       # All pairs in the input
   g      # Filtered by:
    Au-   #  Does the difference
       1= #  Equal 1?
k         # End filter
 e        # Map over the list:
  e       #  Map over the pair
   z0Ah   #   Get the index in the input
       E  # End both maps
e         # Map over this list:
 AuAD     #  Get the absolute difference
     E    # End map
      S   # Sum this list
          # Implicit output

JavaScript (V8), 54 bytes

$=([o,...a])=>o?a.indexOf(o+1)+a.indexOf(o-1)+2+$(a):0

Try it online!

Julia 1.0, 63 55 bytes

Inspired by the answer of xnor

Improved version, thanks @MarcMush:

!x=sum(abs,[(I=indexin)(i,x)-I(i+(i+1∈x),x) for i=x])

Try it online!

First version:

!x=sum(abs.([indexin(i,x)-indexin(i+(i+1 in x),x) for i in x]))

Try it online!

Japt -mx, 16 11 10 bytes

Inspired by xnor's solution.

VaWbU+WøUÄ

Try it

VaWbU+WøUÄ     :Implicit map of each U at 0-based index V in input array W
Va             :Absolute difference of V and
  Wb           :  Index in W of
    U+Wø       :    U + Does W contain
        UÄ     :      U+1
               :Implicit output of sum of resulting array

Jelly, 8 bytes

iⱮ‘aạ¥JS

Try it online!

-1 byte thanks to Unrelated String!

How it Works

iⱮ‘aạ¥JS - Main link. Takes a list L on the left
  ‘      - Increment all elements in L
 Ɱ       - Over each incremented element:
i        -   Get its 1-based index in L, or 0 if not present
           Call this list of indices I
      J  - Indices of L; [1, 2, 3, ..., len(L)]
     ¥   - Last 2 links as a dyad f(I, J):
    ạ    -   Absolute difference, element wise, between I and J
   a     -   And; Where there are non-zeroes in I, replace the value with corresponding difference
       S - Sum

Japt -x, 20 17 12 bytes

ã_ÎaZo)¥1©ZÊ

Try it

    ã_  - subsections passed trough
    ÎaZo) * abs difference of 1st
            and last element(then removed)
    ¥1    * == 1?
    ©ZÊ   * take lenght/else 0
    -x flag to sum

Brachylog, 15 bytes

sᶠ{↻h₂-ȧ1&b}ˢcl

Try it online!

sᶠ                 Find every substring of the input.
  {        }ˢ      Keep only those for which
       ȧ           the absolute value of
      -            the difference of
   ↻h₂             the first and last elements
        1          is 1,
         &b        and remove their first elements.
             c     Concatenate them,
              l    and output their combined length.

MATL, 9 bytes

tQ!=&f-|s

Try it online! Or verify all test cases.

How it works

Consider input [2 9 3 6 8 1] as an example.

tQ   % Implicit input. Duplicate, add 1; element-wise
     % STACK: [2 9 3 6 8 1], [3 10 4 7 9 2]
!    % Transpose
     % STACK: [2 9 3 6 8 1], [3; 10; 4; 7; 9; 2]
=    % Test for equality (element-wise with broadcast)
     % STACK: [0 0 1 0 0 0;
               0 0 0 0 0 0;
               0 0 0 0 0 0;
               0 0 0 0 0 0;
               0 1 0 0 0 0;
               1 0 0 0 0 0]
&f   % Two-output find: row and column indices of nonzeros
     % STACK: [6; 5; 1], [1; 2; 3]
-|   % Minus, absolute value (element-wise)
     % STACK: [5; 3; 2]
s    % Sum. Implicit display
     % STACK: 10

Vyxal s, 13 bytes

›vḟv›ƛ&›[¥ε|0

Try it Online!

A port of the jelly answer, and I think the best I'm gonna get.

Attempted port of Jonas, 21 bytes

Ẋƛ÷ε1=;?Lɾƛ?Lɾ-ȧ;f*∑½

Try it Online!

Old version, 23 bytes

L²(⁰Ẋni÷ε1=[n6ḋ÷ε&+])¥½

Try it Online! I still think there's a bit more I can do.

Older version, 28 bytes

L(x|L(←x i⁰niε1=[n←xε&+]))¥½

Try it Online!

This. Is. Horrible.

Wolfram Language (Mathematica), 42 bytes

Tr@Abs[s@@@Outer[s=#-#2&,#,#]~Position~1]&

Try it online!

Essentially a port of Jonah's J answer.

R, 45 bytes

sum(abs(seq(a<-scan())-match(a+1,a)),na.rm=T)

Try it online!

Merged solutions: mine and @Dominic's.


Previous solution:

R, 48 bytes

x=scan();`*`=match;sum(abs(x*x-(x+1)*x),na.rm=T)

Try it online!

Ruby, 50 48 bytes

->a{r=-1;a.sum{((a.index(a[r+=1]+1)||r)-r).abs}}

Try it online!

Thanks @ovs for -2 bytes.

R, 47 bytes

sum(abs(seq(a<-scan())-(b=match(a+1,a,0)))*!!b)

Try it online! with test wrapper stolen from pajonk's answer stolen from Kirill L's answer

Excel, 52 bytes

=LET(x,A:A,SUM(IFERROR(ABS(ROW(x)-XMATCH(x-1,x)),)))

Input numbers in column A. For each number x in column A look for x-1. If found add the difference in rows between x and x-1.

R, 48 bytes

`?`=diff;sum(abs(?o<-order(x<-scan()))*!1<?x[o])

Try it online!

J, 22 bytes

[:+/&,|@(#\-/#\)*1=-/~

Try it online!

Consider f 2 9 3 6 8 1:

Red, 88 77 bytes

func[b][s: 0 forall b[s: s + absolute offset? b any[find head b 1 + b/1 b]]s]

Try it online!

Uses @xnor's method - don't forget to upvote it!

Red, 129 bytes

func[a][s: 0
b: sort copy a
while[b/2][s: s + either b/2 - b/1 = 1[absolute(index? find a b/2)- index? find a b/1][0]b: next b]s]

Try it online!

sorts the array, and adds the index difference to s if pair of elements has an absolute difference of 1.

Not sure how to shorten the reuse of index? find, but I think it can be shortened. abs alias for absolute is not available, even in version 0.6.4.

JavaScript (Node.js), 60 bytes

n=>n.map((e,i)=>z+=(j=n.indexOf(e+1))+1?j>i?j-i:i-j:0,z=0)|z

Try it online!

Python 2, 60 bytes

lambda l:sum(abs(l.index(x)-l.index(x+(x+1in l)))for x in l)

Try it online!

Charcoal, 15 bytes

I↨¹Eθ↔↨¹⁻⌕Aθ⊕ικ

Try it online! Link is to verbose version of code. Explanation:

    θ           Input array
   E            Map over elements
             ι  Current value
            ⊕   Incremented
         ⌕A     Find all (i.e. at most one) matches
           θ    In input array
        ⁻       Vectorised subtract
              κ Current index
      ↨¹        Take the sum
     ↔          Take the absolute value
 ↨¹             Take the sum
I               Cast to string
                Implicitly print

Note that base 1 conversion is used to sum as this works on empty lists.

05AB1E, 8 bytes

ãʒÆ}kÆÄO

Try it online!

ã          # all pairs of integers from the input
 ʒ }       # keep those where the following results in 1:
  Æ        #   reduce by subtraction
    k      # for each integer in each pair get the index in the input list
     Æ     # reduce each pair of indices by subtraction
      Ä    # take absolute value
       O   # sum all distances

Python 3, 91 bytes

def f(l):c=sorted(l);return sum(abs(l.index(x)-l.index(y))for x,y in zip(c,c[1:])if y-x==1)

Try it online!