4
\$\begingroup\$

Inspired by this game

Challenge:

Given an ASCII art string representing a number, output that number.
Example:

Input:

  &&&&      #####    .====..    .&&&
 $$$$$.    %%   %%   %    % .   +. .
   @@@  .      @@ .  = .  =   . ????
   +++.    &&   &&.  %    %     =   =
   $$$    . @@@@@    #    #  .  ===.
 .$$$$$              .????.

Output: 1306
Input:

  ====     .$$..      &&&&&&.    .@@@@    ?????
 +++++     &. &           $$    %%   %%  &&   &&
   $$$     .==.          @@ .   @@   @@ .    ??
.  ### .   @  @  .      ==     $$$$$$$$  ??. .??
. .@@@.    .%%.        ==           .$$   &&&&&  .
  $$$$$. .            .%        .    @@ .

Output: 18743

Periods are interference, you can ignore them
The complete list of numbers:

 &&&& 
%    %
=    =
%    %
#    #
 ????

 &&&&
$$$$$
  @@@
  +++
  $$$
 $$$$$


 #####
++   ++
 #  ##
   @@
  @@    
???????


 #####
%%   %%
    @@
&&   &&
 @@@@@  


   @@@@
 %%   %%
 @@   @@
$$$$$$$$
      $$
      @@ 


###### 
$  
#### 
    + 
    ? 
$$$$$ 


 &&&
+ 
????
=   =
===

&&&&&&
    $$
   @@
  ==
 ==
 %

 $$
&  &
 ==
@  @
 %%


 &&&&
@    @
 &&&&
    $
    &
  + 

Rules:

  • Your input is a string unless your language doesn't have a string type.
  • The input is guaranteed to be exactly 6 characters "high." Numbers such as 8 and 3 have a whitespace buffer UNDER the number.
  • If you write a function, it MUST return a numerical type.
  • The ASCII art can be composed of ANY printable characters in no particular order (remember, you can ignore periods).
  • No standard loopholes.
  • Your program must handle any arbitrary ASCII number of reasonable length (as long as the result fits within a 32-bit signed integer).
  • Input is guaranteed valid, undefined behavior is allowed with bad input.
  • This is code-golf, shortest program wins.

    More tests:
  $$$$     ######     @@@@  . .  .%%%%    #####
 #####     $ .       =====..  . ##   ##  ++   ++
   +++     ####.       +++.     ==   ==   #  ##
   ===     .   +       ###  .  ========.    @@
  .&&&    .  . ?   . . @@@           ##    @@    .
  %%%%%    $$$$$      #####          @@  ???????

> 15142

 .&&&&.    ??????     @@@@@      .++++
 @    @    + .       ==   ==    %%   %%
 .&&&&.    ####..     =  == .   ++   ++
  .. $         &        $$     $$$$$$$$.
 .   &.      . #       && .          ##
   +.   .  #####     $$$$$$$        .==

> 9524
\$\endgroup\$
20
  • 1
    \$\begingroup\$ As far as I can tell, the set of valid number characters is &$@+#%=?. Are there any other characters that are valid? Will each row in a number always contain the same character? \$\endgroup\$
    – Stephen
    Mar 18, 2019 at 19:03
  • 1
    \$\begingroup\$ @Stephen I updated the question. The numbers can be composed of any printable characters in no particular order. \$\endgroup\$ Mar 18, 2019 at 19:31
  • 1
    \$\begingroup\$ Can a period . appear as both a part of the character and a part of interference? \$\endgroup\$ Mar 18, 2019 at 19:43
  • 3
    \$\begingroup\$ I'm not sure that "periods are interference" really adds much to the challenge. \$\endgroup\$ Mar 18, 2019 at 20:59
  • 2
    \$\begingroup\$ The 2 in the tests is different to the 2 in the sample digits \$\endgroup\$ Mar 18, 2019 at 22:13

3 Answers 3

4
\$\begingroup\$

Jelly, 35 33 34 bytes

Ỵe€€⁾ .¬ZḄṣ0§ḟ0%29“h½sØ?X’ḃ26¤iⱮ’Ḍ

Try it online!

When I started writing this, The question didn’t specify that the width of digits was fixed, so my answer doesn’t make that assumption.

Input is now a string (which costs a byte over having a list of strings). Output is an integer.

Explanation:

Ỵ                         | split at new lines
e€€⁾ .                    | For each character check whether it is a
                          | full stop/space or something else
      ¬                   | invert this
       Z                  | transpose
        Ḅ                 | convert from binary to decimal (one number per column of input)
         ṣ0               | split at zeroes (blank columns)
§                         | take the sum of each digit
 ḟ0                       | filter out remaining blank columns
   %29                    | take this number mod 29
      “h½sØ?X’ḃ26¤iⱮ      | look each digit up in a compressed list
                          | [18, 23, 6, 5, 1, 19, 13, 21, 8, 25]
                    ’     | subtract one
                     Ḍ    | convert to decimal
\$\endgroup\$
2
  • \$\begingroup\$ Minor note: The post is worded as "input must be a string", which I read as allowing a list of characters or taking input as a full program. As such I would ask if we may take a list of the lines. \$\endgroup\$ Mar 20, 2019 at 12:44
  • \$\begingroup\$ @JonathanAllan fair enough. added following response. \$\endgroup\$ Mar 20, 2019 at 15:21
2
\$\begingroup\$

05AB1E, 34 bytes

'.ð:.B€SðÊøJC0¡O0K29%•!aˆM©м•₂вskJ

Port of @NickKennedy's Jelly answer, so make sure to upvote him!
PS: Knowing the width of the digits is pretty useless if the amount of space columns in between digits can vary..

Try it online.

Explanation:

'.ð:                              '# Replace all dots with spaces
    .B                             # Split on newlines
      €S                           # Convert each line to a list of characters
        ðÊ                         # Check of each character if it's NOT equal to a space
          ø                        # Zip/transpose; swapping rows/columns
           J                       # Join each column together
            C                      # Convert each from binary to an integer
             0¡                    # Split on 0s
               O                   # Sum each inner list
                0K                 # Remove 0s
                  29%              # Take modulo-29 on each
                     •!aˆM©м•      # Push compressed integer 102583844953589
                             ₂в    # Converted to Base-26 as list: 
                                   #  [18,23,6,5,1,19,13,21,8,25]
                               sk  # Index the earlier number into this list
                                 J # Join everything together (and output implicitly)

See this 05AB1E tip of mine (sections How to compress large integers? and How to compress integer lists?) to understand why •!aˆM©м• is 102583844953589 and •!aˆM©м•₂в is [18,23,6,5,1,19,13,21,8,25].

\$\endgroup\$
2
\$\begingroup\$

Jelly, 29 bytes

⁾. yỴ=⁶s€⁵ZẎ€Ḅ:434ị“¡£:E’Œ?’¤

A monadic Link accepting a list of characters which yields a list of digits, or a full program accepting a single text argument which prints the digits.

(Requires padding on the right with spaces such that each digit has a width of ten characters, including the right-most one, which I believe is OK.)

Try it online! ...or try the other test case here.

\$\endgroup\$
8
  • \$\begingroup\$ Are you sure this works correctly? I copied the first test case, and it gives [1,3,4,8] instead of [1,3,0,6]. And I copied the second test case and it gives [1,5,4,4,4] instead of [1,8,7,4,3].. \$\endgroup\$ Mar 20, 2019 at 12:54
  • \$\begingroup\$ @KevinCruijssen I copied the digits from the list yesterday, pre-pended a single space to each line of each of them and placed them together with a fixed-width of 10. The code does require trailing padding to 10 chars, but it looks like something else is different about the tests vs what I've done. I can't look now but if you can tell from my TIO input do let me know; I'll look later today... \$\endgroup\$ Mar 20, 2019 at 13:36
  • \$\begingroup\$ @KevinCruijssen ...yeah the 2 has changed since yesterday! try it \$\endgroup\$ Mar 20, 2019 at 13:41
  • \$\begingroup\$ @KevinCruijssen I believe the 1306 is formatted incorrectly and has the 6 too far to the right. try this \$\endgroup\$ Mar 20, 2019 at 13:49
  • \$\begingroup\$ ...this was exactly why I asked about the format yesterday :) \$\endgroup\$ Mar 20, 2019 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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