7-segment differences
JavaScript (ES6), 46 40 bytes
f=n=>n?+"452331541"[n%10]||f(n/10|0)+2:2
Alternative formulation, also 46 40 bytes:
f=n=>n?26523308>>n%10*3&7||f(n/10|0)+2:2
Edit: Saved 6 bytes thanks to @xsot.
Python, 50 48 bytes
f=lambda n:26523308-0**n*2>>n%10*3&7or f(n/10)+2
Explanation
This function operates on the least significant digit of the number n
, summing the 7SD of the digits when incremented by one until after the first non-9
digit.
26523308
is a bitmask that encodes the mapping for the digits 0-8
. When n=0
, which only occurs when n
comprises only 9
s, the answer will be off by two. This is compensated by the expression 0**n*2
. As for the digit 9
, the bitmask evaluates to zero which will trigger the recursive call while adding 2
to the 7SD.
05AB1E, 31 30 28 27 26 bytes
Code:
9Ü©T%•2X›ùì•sè¹g®g-·¹Ú9Q·O
Explanation (outdated):
9Ü # Trim off trailing 9's
© # Copy this into the register
T% # Get the last non-9 digit
žh # Short for 0123456789
•2X›ù앧 # Compressed version of 4523315412
‡ # Transliterate
We are changing the following to the last non-9 digit:
0 -> 4
1 -> 5
2 -> 2
3 -> 3
4 -> 3
5 -> 1
6 -> 5
7 -> 4
8 -> 1
9 -> 2
For the special cases:
¹g # Get the length of the input
®g # Get the length of the input with all trailing 9 gone
- # Substract, giving the number of 9's at the end of
the input
2* # Multiply by two
O # Sum everything up
¹Ú # Uniquify the input
9Qi # If this is equal to 9 (only 9's in the input)
Ì # Increment by 2 (_ -> 1)
Uses the CP-1252 encoding. Try it online!.
28 byte alternative: D[¤©•2X›ùì•sès®9Ê#¨]\rÚ9Q4*O
.