Expand a number

Your task is to expand an integer greater than 0 in the following manner:

Split the number into decimal digits and for each digit generate a list according to these rules:

• if the digit is odd, the list starts with the digit and goes down to 1;
• if the digit is even, the list starts with the digit and goes up to 9.
• if the digit is 0, the list is empty.

Write down the lists for the odd numbers below the digits, and above for the even ones. Then top-align the columns and collect the digits in each row to make integers. As a final step add up the numbers to find the expansion of the number.

Here's an example of the above rules applied to 34607:

9
8
79
68
57
346 7 ->  399 7 -> 3997 -> 9418
2   6     288 6    2886
1   5     177 5    1775
4      66 4     664
3      5  3      53
2      4  2      42
1         1       1

Here are the test cases:

1: 1
2: 44
3: 6
44: 429
217: 1270
911: 947
2345: 26114
20067: 3450
34875632: 70664504
9348765347634763: 18406119382875401

This is , so the shortest answers in bytes in each language win.

• Can we take input as a string? Or as an array of digits? Nov 8 '17 at 21:56
• @Arnauld It must be an integer, and to split it to digits with your program/function Nov 8 '17 at 21:59
• @GalenIvanov But if input is from stdin, I suppose that's ok (even though technically a string), right?
Nov 8 '17 at 22:05
• @Adám Yes, technically it's a string, so that's ok. Nov 9 '17 at 6:03
• And here I thought it would be an expansion like this. Nov 9 '17 at 14:29

Jelly, 13 bytes

Dḟ0RrḂ?€9UZḌS

Try it online! or see the test-suite.

How?

Dḟ0RrḂ?€9UZḌS - Link: positive integer           e.g. 702
D             - cast to a decimal list                [7,0,2]
0           - literal zero                          0
9     - literal nine
€      - for each:
?       -   if:
Ḃ        -   ...condition: bit (modulo by 2)      1              ,0
R          -   ...then: range ([1,...n])            [1,2,3,4,5,6,7],n/a
r         -   ...else: inclusive range ([n,...9])  n/a            ,[2,3,4,5,6,7,8,9]
U    - upend                                 [[7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2]]
Z   - transpose                             [[7,9],[6,8],[5,7],[4,6],[3,5],[2,4],[1,3],2]
Ḍ  - cast from decimal lists               [79,68,57,46,35,24,13,2]
S - sum                                   324

Perl 6,  68  66 bytes

{sum roundrobin(.comb».&{$_%2??($_...1)!!(9...+$_) if +$_})».join}

Try it

{sum roundrobin(.comb».&{[R,] $_%2??1..$_!!$_..9 if +$_})».join}

Try it

Expanded:

{  # bare block lambda with implicit parameter ｢$_｣ sum roundrobin( # grab from the sub lists in a round robin fashion .comb\ # split the input into digits ».&{ # for each digit do this [R,] # reduce with reversed &infix:«,» (shorter than reverse)$_ % 2     # is the digit not divisible by 2?
??  1  .. $_ # from 1 to the digit !!$_ ..  9   # from the digit to 9

Try it online!

Python 2, 131 bytes

lambda n:sum(int(''.join(nfor n in l if n))for l in map(None,*[range(n and(n%2*n or 9),(n%2<1)*~-n,-1)for n in map(int,n*10)]))

Try it online!

05AB1E, 16 bytes

0KεDÈi9ŸëL]íõζJO

Or alternatively:

0KεDÈ8*>Ÿ{R}õζJO
0Kε9Ÿ¬L‚yèR}õζJO

Explanation:

0K        # Remove all 0s from the (implicit) input-integer
ε       #  Map each digit to:
D      #   Duplicate the digit
Èi    #   If it's even:
9Ÿ  #    Pop and push a list in the range [digit, 9]
ë    #   Else (the digit is odd):
L   #    Pop and push a list in the range [1, digit]
]       # Close both the if-else statement and map
í      # Reverse each inner ranged list
ζ    # Zip/transpose, swapping rows and columns,
õ     # with an empty string as filler
J   # Join each inner list together
O  # And sum that list
# (after which the result is output implicitly)

Burlesque, 39 bytes

riXX:nz{J2dv{{9r@}{ro}}che!<-}m[tp)im++

Try it online!