Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This challenged is highly inspired by what @Mego created with his Holy and Holier numbers, lots of thanks to him and his puns.

Holy numbers are numbers composed of only the digits with holes, which are:

04689

Numbers with at least one unholy digit are considered unholy. Unholy digits are evil by definition, but being close to holy digits help them become neutral. Hence, the closer they are, the less unholy (1 when adjacent).

The unholiness of a number is the sum of the unholyness of its digits, a number composed of only unholy number has an infinite unholiness.

Number            :8 5 5 8 7
Digital Unholiness:0+1+1+0+1
Total Unholiness  :3

Number            :0 1 7 5 5 2 8 5 7 
Digital Unholiness:0+1+2+3+2+1+0+1+2
Total Unholiness  :12

Number            :1 5 7 3 2 1
Digital Unholiness:∞+∞+∞+∞+∞+∞
Total Unholiness  :∞

Number            :0 4 6 8 9
Digital Unholiness:0+0+0+0+0
Total Unholiness  :0

Your task

You have to write a program or function that takes a positive integer or a string only composed of digits as input, and output its unholiness. If you chose to use an integer as input, you can assume it will never have a leading 0 as your language may drop it.

In case of infinite unholiness, you can chose between three outputs

  • The character (3 bytes)
  • Infinite output containing at least 1 non-zero digit, but only digits.
  • A built-in Infinity value.

This is code-golf, so the shortest code in byte wins, good luck!

share|improve this question
    
Is returning a built-in Infinity value legal? – Neil Mar 2 at 9:25
1  
@Neil I will allow it, as I didn't even think of it, good point there. – Katenkyo Mar 2 at 10:56
    
Some of your example inputs start with a leading zero; is it intended that we can write our function with input "a positive integer" only if our language of choice won't automatically drop the leading zero? Many languages will be forced to take string input for this reason. – A Simmons Mar 2 at 11:36
    
@ASimmons That's why I modified (a while back) the input so it can be a "string only composed of digits" aswell. Also, the important point isn't that it is a 0 but a holy digit, I will modify the post according to allow answer based on non-leading 0 numbers. – Katenkyo Mar 2 at 12:26
    
@katenkyo Yeah I saw you could input as a string but it seemed hard to take it as an integer. I approve of your edit to the OP. – A Simmons Mar 2 at 13:56
up vote 2 down vote accepted

MATL, 25 24 bytes

7Zq1hVmt~f!wf-|X<st~?xYY

Try it online!

Input is a string. In the output, infinity is natively displayed as Inf.

Explanation

7         % number literal                                 
Zq        % prime numbers up to a 7: [2 3 5 7]
1         % number literal                        
h         % horizontal concatenation                       
V         % convert numbers to string: '2  3  5  7  1'
m         % take input implicitly. Determine which digits are 1,2,3,5,7
t         % duplicate
~         % element-wise negate: which digits are 4,6,8,9,0
f         % indices of occurrences of digits 4,6,8,9,0
!         % transpose into column array
w         % swap elements in stack           
f         % indices of occurrences of digits 1,2,3,5,7  
-         % element-wise subtraction with broadcast. Gives 2D array
|         % element-wise absolute value                          
X<        % minimum of each column
s         % sum of elements of array
t         % duplicate                       
~         % element-wise negate
?         % if all elements are true                            
  x       %   delete                                         
  YY      %   push infinity                                       
          % (implicit) end if
          % (implicit) convert to string and display  
share|improve this answer

Python (3), 137 131 bytes

def f(s):
 l=range(len(s))
 r=[min(i)for i in zip(*[[abs(j-i)for j in l]for i in l if s[i]in'46890'])]
 return sum(r)if r else'∞'

Results

>>> [f(i) for i in ['85587', '012321857', '157321', '04689']]
[3, 12, '∞', 0]
share|improve this answer
    
I get a count of 131 Bytes, Is there something I am missing? Also, great answer :). – Katenkyo Mar 2 at 10:59
    
@Katenkyo I always forgot my editor add en empty line at the end of the file – Erwan Mar 2 at 11:40

Pyth, 31 29 27 25 bytes

smhS.e?}b"04689"akd.n4zUz

Try it online: Demonstration or Test Suite

For each digit I compute the distances to each number. A distance is infinty, if the second digit is not holy. From these lists I take the minimal distance and sum it up.

Explanation:

smhS.e?}b"04689"akd.n4zUz  implicit: z = input string of numbers
 m                     Uz  map each d in [0, 1, ..., len(z)-1] to:
    .e                z      map each k (index), b (value) of b to:
                akd            absolute difference between k and d

      ?}b"04689"               if b in "04689" else
                   .n4         infinity
   S                           sort
  h                            take the first element (=minimum)
s                              print the sum
share|improve this answer

JavaScript (ES6), 93 bytes

s=>[...s].map(n=>/[12357]/.test(n)?++u:u=0,u=1/0).reverse().map(n=>r+=n?n<++u?n:u:u=0,r=0)&&r

If Infinity isn't a legal infinity, then add 13 bytes for ==1/0?'∞':r.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.