12
\$\begingroup\$

Yesterday, I left my sandwich on the table. When I got up today, there was a bite in it... Was it mine? I can't remember...

Problem:

Take a representation of the sandwich and my bite pattern and tell me if it was my bite or not.

Examples:

Example 1:

My bite pattern:

..
.

Sandwich:

#####
.####
..###

Output:

truthy

Example 2:

My bite pattern:

..
..

Sandwich:

...##
..###
.####

Output:

falsy

Example 3:

If there is at least 1 rotation that counts as truthy, the output is truthy.

My bite pattern:

.
 .
  .

Sandwich:

##.
#.#
.##

Output:

Two possible rotations (biting in the northeast or southwest corner).

truthy

Some valid bites:

..
.

...
.
.

.
 .
  .

..
. .
 ..

 ..
.
. .

Some invalid bites:

..

...
.

..
.
 .

Rules:

  • My bite pattern orientation will always be for biting the northwest corner. And must be rotated to bite other corners;

  • There will always be 1 and only 1 bite in the sandwich;

  • The bite in the sandwich can be in any of the 4 cornes (rotated accordingly);

  • Bite patterns will always be symmetrical along the main diagonal;

  • Bite patterns will always be at least 1 wide and non empty;

  • The sandwich will always be a rectangle with width and height equal or greater than the width of my bite pattern;

  • In your input, you can choose any 2 distinct non-whitespace characters to represent the sandwich and the bite;

  • Spaces in the bite pattern means that my bite does not touch that part of the sandwich.

\$\endgroup\$
  • \$\begingroup\$ Can the bite pattern be larger than the sandwich? Can the bite pattern be empty? Can the bite pattern be the same as the sandwich? i.e. .., ..? \$\endgroup\$ – TheLethalCoder May 19 '17 at 12:48
  • \$\begingroup\$ @TheLethalCoder the rules say the bite pattern will always fit the sandwich. i'll add a new rule to specify minimum size (1 width) \$\endgroup\$ – Felipe Nardi Batista May 19 '17 at 12:50
  • \$\begingroup\$ @TheLethalCoder and yes, the bite pattern can be the same as the sandwich \$\endgroup\$ – Felipe Nardi Batista May 19 '17 at 12:51
2
\$\begingroup\$

Ruby, 103 bytes 101 bytes

->b,s{[a=s.map(&:reverse),s,s.reverse,a.reverse].any?{|t|b.zip(t).all?{|y,x|y==x.tr(?#,' ').rstrip}}}

Try it online!

Saved 2 bytes by moving the assignment to the first use of a. Apparently Ruby is smart enough to not confuse the commas in the array definition and commas that would arise from simultaneous variable assignment (at least in this case :D)

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

Python 2, 134 bytes

b,s=input()
a=[''.join(l[::-1])for l in s]
print any(b==[l.replace('#',' ').rstrip()for l in x][:len(b)]for x in(a,a[::-1],s[::-1],s))

Takes input as two lists of strings (one for each line). Assumes no trailing whitespace on the lines.

Try it online!

Examples:

Input: ['..','.'],['#####','.####','..###'] (example 1)
>True

Input: ['..','..'],['...##','..###','.####'] (example 2)
>False

Input: ['',' .'],['#####','#.###','#####'] (no bite in top row)
>True
\$\endgroup\$
1
\$\begingroup\$

Python 2, 173 bytes

Try it online

S,b=input()
L=len
B=map(lambda x:[y<'.'and'#'or y for y in x]+['#']*(L(S[0])-L(x)),b+[[]]*(L(S)-L(b)))
R=map(lambda x:x[::-1],B)
print S==B or S==B[::-1]or S==R[::-1]or S==R

Takes input as two lists of lists of characters.
First - sandwich
Second - bite

First it extends bite array to the size of sandwich array:

B=map(lambda x:[y<'.'and'#'or y for y in x]+['#']*(L(S[0])-L(x)),b+[[]]*(L(S)-L(b)))

[y<'.'and'#'or y for y in x] replaces all spaces to #
(L(S[0])-L(x)),b+[[]]*(L(S)-L(b)) calculates the number of missing element

Then it compares all 4 rotation of this "extended" bite to the sandwich:

R=lambda:map(lambda x:x[::-1],B)
print S==B or S==B[::-1]or S==R()or S==R()[::-1]
print any(map(S.__eq__,[B,B[::-1],R(),R()[::-1]])) #longer but pretty

lambda R is used to mirror list of lists horizontally

In the linked example sandwich is:

##.
#.#
###

And bite is:

.
 .
\$\endgroup\$
  • 1
    \$\begingroup\$ why R=Lambda:map... and not R=map... \$\endgroup\$ – Felipe Nardi Batista May 19 '17 at 15:37
  • \$\begingroup\$ @FelipeNardiBatista Because I missed it :D Thanks! \$\endgroup\$ – Dead Possum May 19 '17 at 15:44
  • \$\begingroup\$ B=[[y<'.'and'#'or y for y in x]+['#']*(L(S[0])-L(x))for x in b+[[]]*(L(S)-L(b))] for -4 \$\endgroup\$ – ovs May 20 '17 at 5:55
  • \$\begingroup\$ And R=[x[::-1]for x in B] for -4 as well. In general, don't use map with a lambda function \$\endgroup\$ – ovs May 20 '17 at 5:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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