9
\$\begingroup\$

I have a bunch of ASCII art that I have recovered from a failed hard drive. Unfortunately, all of the newlines were stripped out, and I don't know the resolution of the original artwork. I do know that each one was originally rectangular, but to make matters worse, all the white space at the beginning and end was stripped out too!

I do at least know what one small piece of each one looks like. Can you help me recover the artwork based on the input stream of bytes and the piece of ASCII art I remember?

Example

I remember this one:

__(((55^$$%^/\__%%//\/_666332@12

Had this diamond in it:

/\
\/

The original artwork in this case was 8 characters wide:

__(((55^
$$%^/\__
%%//\/_6
66332@12

Then there's this one, which had a bigger diamond in it.

/\/\/\/\/\:;/  \00F\/\/\  /E%epopox\/e

The diamond looked like this:

 /\
/  \
\  /
 \/

The original artwork contained that diamond, but the white space surrounding the diamond was not white space in the original artwork. I’ve only added in the white space to the left to get the indentation right.

More generally, in each row, any space to the left of the first non-white-space character is indentation padding, and any white space to the right necessarily represents actual white space.

Note that this is also one of those pieces that has had its white space trimmed off. Here's the original, 11 characters wide, with white space restored:

   /\/\/\/\
/\:;/  \00F
\/\/\  /E%e
popox\/e   

Remember, the amount of white space removed is unknown. That means I can’t even figure out the possibilities for line length by finding the divisors of the input’s character count.

Input

A string of characters (the artwork, sans newlines, trimmed of white space at the beginning and end), and a two-dimensional string of characters (the piece to look for)

Essentially, the string of characters can be generated from the original artwork thus: artwork.Replace("\n", "").Trim(" ")

Output

The original artwork with newlines inserted where necessary, and with trimmed white space re-added to the beginning of the first line and the end of the last line. If there is more than one possible solution, you may return one arbitrarily (or all of them if you really want to).

Test Cases

Input:
__(((55^$$%^/\__%%//\/_666332@12
/\
\/

Output:
__(((55^
$$%^/\__
%%//\/_6
66332@12

-----------
Input:
 _-/\-_-_vv:;/  \00F\\||\  /E%epopox\/e0
 /\
/  \
\  /
 \/

Output:
   _-/\-_-_
vv:;/  \00F
\\||\  /E%e
popox\/e0  


--------
Input:
__               (  )               ||                ||            ___|""|__.._     /____________\    \____________/~~~.
(  )
 ||
 ||
 ""

Output:
     __           
    (  )          
     ||           
     ||           
 ___|""|__.._     
/____________\    
\____________/~~~.


--------
Input:
..XX...XX.......X..X..X  X.....X..X...X  X.....XX......XX...
 XX 
X  X
X  X
 XX 

Output:
..XX...XX.......
X..X..X  X.....X
..X...X  X.....X
X......XX...    

Notably, in this last case, the following is not valid output:

..XX...XX......
.X..X..X  X....
.X..X...X  X...
..XX......XX...

Credit to jrei @ asciiart.eu for the joystick

Rules

  • This is code golf, so shortest answer in bytes wins.
  • Default I/O rules apply
\$\endgroup\$
  • 2
    \$\begingroup\$ Is it a problem if we return some extra row of white spaces at the begin/end? Can the pattern have a row/column of white spaces at the begin/end? \$\endgroup\$ – user202729 Oct 18 at 2:42
  • 3
    \$\begingroup\$ Is it guaranteed that the output is rectangular (all row have the same length)? \$\endgroup\$ – user202729 Oct 18 at 2:43
  • 1
    \$\begingroup\$ Is it OK if we assume that any space in the original artwork may have been replaced with another character, or are we supposed to make sure that only surrounding spaces have been replaced? \$\endgroup\$ – Arnauld Oct 18 at 9:28
  • 1
    \$\begingroup\$ @Jonah he answered that before, scroll up a few comments. \$\endgroup\$ – Kroppeb Oct 18 at 15:01
  • 1
    \$\begingroup\$ @Jonah As noted in the original post, yes, default I/O rules apply. \$\endgroup\$ – Ed Marty Oct 19 at 4:58
4
\$\begingroup\$

Perl 5, 192 bytes

To be golfed more

($_,@F)=@F;$l=max map length,@F;map{$_.=" "x($l-y///c);s/(^ +| +$)|\W/$1=~y, ,.,r||"\\$&"/gme}@F;for$i(0..y///c){$"=".{$i}";$i+=$l,$_=" "x($i-"@-").$_,map{printf"%-$i\s
",$_}/.{1,$i}/g if/@F/}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Can this be adjusted to make sure it outputs a rectangular image? It’s missing the extra space on the last line. \$\endgroup\$ – Ed Marty Oct 18 at 10:59

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.