Was this bite mine?

12

1

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.

Felipe Nardi Batista

Posted 2017-05-19T11:59:22.403

Reputation: 2 345

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. .., ..? – TheLethalCoder – 2017-05-19T12:48:23.087

@TheLethalCoder the rules say the bite pattern will always fit the sandwich. i'll add a new rule to specify minimum size (1 width) – Felipe Nardi Batista – 2017-05-19T12:50:21.383

@TheLethalCoder and yes, the bite pattern can be the same as the sandwich – Felipe Nardi Batista – 2017-05-19T12:51:56.900

Answers

2

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)

Jenkar

Posted 2017-05-19T11:59:22.403

Reputation: 211

2

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

TFeld

Posted 2017-05-19T11:59:22.403

Reputation: 19 246

1

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:

.
 .

Dead Possum

Posted 2017-05-19T11:59:22.403

Reputation: 3 256

1why R=Lambda:map... and not R=map... – Felipe Nardi Batista – 2017-05-19T15:37:45.987

@FelipeNardiBatista Because I missed it :D Thanks! – Dead Possum – 2017-05-19T15:44:04.467

B=[[y<'.'and'#'or y for y in x]+['#']*(L(S[0])-L(x))for x in b+[[]]*(L(S)-L(b))] for -4 – ovs – 2017-05-20T05:55:00.270

And R=[x[::-1]for x in B] for -4 as well. In general, don't use map with a lambda function – ovs – 2017-05-20T05:59:02.507