25
\$\begingroup\$

Write a program or function that takes in a positive integer N and a grid of decimal digits (0 to 9) with width W and height H (which are also positive integers). You can assume that N will be less than or equal to the larger of W and H.

Print or return the largest contiguous N digit number that appears horizontally or vertically in the grid, written in normal reading order or in reverse.

  • Diagonal lines of digits are not considered.
  • The grid does not wrap around, i.e. it does not have periodic boundary conditions.

For example, the 3×3 grid

928
313
049

would have 9 as the output for N = 1, 94 as the output for N = 2, and 940 as the output for N = 3.

The 4×3 grid

7423
1531
6810

would have 8 as the output for N = 1, 86 for N = 2, 854 for N = 3, and 7423 for N = 4.

The 3×3 grid

000
010
000

would have output 1 for N = 1, and 10 for N = 2 and N = 3 (010 is also valid for N = 3).

The 1×1 grid

0

would have output 0 for N = 1.

You can take the input in any convenient reasonable format. e.g. the grid could be a newline separated string of digits, or a multidimensional array, or a list of lists of digits, etc. Leading zeros are allowed in the output if they were part of the grid.

This is , so the shortest code in bytes wins, but I'll also award brownie points (i.e. more likely upvotes) for answers that can show that their algorithm is computationally efficient.

\$\endgroup\$
  • 1
    \$\begingroup\$ Are we allowed to print any leading zeroes? \$\endgroup\$ – PurkkaKoodari Nov 6 '15 at 7:22
  • \$\begingroup\$ @Pietu1998 "Leading zeros are allowed in the output if they were part of the grid." \$\endgroup\$ – Calvin's Hobbies Nov 6 '15 at 15:12
0
\$\begingroup\$

Pyth, 22 19 bytes

3 bytes thanks to Jakube.

seSs.:RQ.n,L_MdCB.z

Try it online.

If we are allowed to print leading zeroes, the code is 18 bytes:

eSs.:RQ.n,L_MdCB.z
\$\endgroup\$
  • \$\begingroup\$ Converting a string with leading zeros to an integer can be accomplished with s. \$\endgroup\$ – Jakube Nov 7 '15 at 14:15
9
\$\begingroup\$

CJam, 39 36 35 34 bytes

qN/)i\[{zW%_}4*]ff{_,@e<ew:i}e_:e>

Just quickly, before @Dennis wakes up :P

Try it online.

Explanation

The basic algorithm is to take all four rotations of the grid and split each row into chunks of length N (or the row length, whichever's smaller). Then convert the chunks to ints and take the largest.

qN/             Split input by newlines, giving an array of lines
)i\             Drop N from the array and put at bottom
[        ]      Wrap in array...
 {    }4*         Perform 4 times...
  zW%_              Rotate grid anticlockwise and push a copy
                Note that this gives an array of 5 grids [CCW1 CCW2 CCW3 CCW4 CCW4]
ff{         }   For each grid row, mapping with N as an extra parameter...
   _,             Push length of row
     @e<          Take min with N
        ew        Split into chunks
          :i      Convert to ints
e_              Flatten that array
:e>             Take cumulative max
\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, does few do anything special, or is it three separate commands? \$\endgroup\$ – ETHproductions Nov 6 '15 at 4:37
  • 3
    \$\begingroup\$ @ETHproductions It's actually the operator ew applied using f, or "map with extra parameter". For example, ["abcd" "efgh"] 2 few results in [["ab" "bc" "cd"] ["ef" "fg" "gh"]]. \$\endgroup\$ – Sp3000 Nov 6 '15 at 4:39
  • \$\begingroup\$ Gotcha :) That's an interesting coincidence, though. \$\endgroup\$ – ETHproductions Nov 6 '15 at 4:54
  • \$\begingroup\$ Only issue is that, when @Dennis wakes up, everybody else loses anyway. ;) \$\endgroup\$ – kirbyfan64sos Nov 6 '15 at 13:35
-2
\$\begingroup\$

Burlesque

Not a final answer yet but it probably will work like this:

blsq ) "7423\n1531\n6810"ln)XXJ)\[jtp)\[_+J)<-_+{3.+ti}m[>]
854
blsq ) "7423\n1531\n6810"ln)XXJ)\[jtp)\[_+J)<-_+{4.+ti}m[>]
7423

How is N and the grid given exactly?

\$\endgroup\$
  • \$\begingroup\$ One should typically wait to post an answer until it's works. Any questions for the OP should be given as comments on the post. \$\endgroup\$ – Alex A. Nov 6 '15 at 20:54
  • \$\begingroup\$ The code actually works. \$\endgroup\$ – mroman Nov 7 '15 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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