3
\$\begingroup\$

Sokobunny 1

You love buns! You love them so much, that you chased one down a bunburrow. The burrows are like mazes, and YOU WANT TO CATCH THE FLUFFBALLS!!!! However, the buns are smart! You can't catch the bun by by chasing after it, as you wait out of politeness for the buns.

Taken from the game Paquerette Down the Bunburrows (This is imo easily the best puzzle game out there, as it is meta meta meta meta ... echoes until inaudible)

Bun behavior

  • Buns will run straight away from you if they can, until they reach a tile with a non-wall left or right. It they see no exits, they run straight ahead.
  • A bun is comfortable until you are closer than two squares (manhattan), this means a bun will keep a distance of two empty squares horizontally and vertically, but won't care about diagonals.
  • A bun won't choose to go into a dead end it can see.
  • A bun can see horizontally and vertically and and tell if it is a straight dead end.
  • A bun can see all adjacent squares (horiz, vert, and diag)
  • A bun will prefer to turn left, if given the choice.
  • A bun is captured if on the same tile as a player.

Challenge:

Shortest wins!

  • Global guarantees: No buns will ever be harmed in the making of these challenges.
  • Guarantees (this challenge only):
    • no bun will be captured
    • no bun will run out of bounds
    • mazes will always be a rectangle
  • Inputs:
    • Ascii Maze: # is a wall, and is not
    • Player position. (Col, row), starting from top-left at (0, 0)
    • Bunny position. Same scheme
  • Outputs:
    • New bunny position
    • All rotated inputs should output the same relative position
  • You must treat any non # character as a wall. This applies expressly to the P and B characters.

the bun after it moves (if it does).

Visual representation (Not valid for challenge):
B is bun, b is where bun goes.
███████████████████████████████████████
████████████████████████   ███   ██████
█████████████████████               ███
██████████████████████████████   ██████
██████          P     B------->b    ███
████████████   ████████████████████████
███████████████████████████████████████

Maze:
#############
######## # ##
#######     #
########## ##
##   P B    #
#### ########
#############
Player: (5, 4)
Bun:    (7, 4)

Output: (10, 4)

Maze:
#############
######## # ##
#######     #
########## ##
##      P B #
#### ########
#############
Player: (8, 4)
Bun:    (10, 4)

Output: (10, 2)

Maze:
############################
#P B                       #
######################### ##
############################
Player: (1, 1)
Bun:    (3, 1)

Output: (25,1)

Maze:
######
### ##
#P B##
###  #
######
Player: (1, 2)
Bun:    (3, 2)

Output: (3, 3)

Maze:
#######
###  ##
## B###
##  ###
###P###
#######
Player: (3, 4)
Bun:    (3, 2)

Output: (2, 2)

Maze:
#######
#P  B #
#######

Player: (1, 1)
Bun:    (4, 1)

Output: (4, 1)

Maze:
#######
# P B #
#######

Player: (2, 1)
Bun:    (4, 1)

Output: (5, 1)

Maze:
#######
#  PB #
#######

Player: (3, 1)
Bun:    (4, 1)

Output: (5, 1)
```
\$\endgroup\$
14
  • 2
    \$\begingroup\$ Someone should answer this in JavaScript (Bun) \$\endgroup\$
    – noodle man
    Sep 14, 2023 at 18:35
  • 1
    \$\begingroup\$ @JonathanAllan I think it's left relative to the direction away from you. \$\endgroup\$
    – xnor
    Sep 14, 2023 at 18:51
  • 2
    \$\begingroup\$ "A bun is comfortable until you are closer than two squares (manhattan)" It seems like being exactly 2 squares from you in a straight line causes the bun to run? Does this apply if the bun is diagonally 1 space away from you? What happens then? \$\endgroup\$
    – xnor
    Sep 14, 2023 at 19:30
  • 2
    \$\begingroup\$ I'm voting to close as there's lacking clarity behind the distance a bun is comfortable for. I'll rescind my vote if this is clarified. (Perhaps provide a diagram of 'threatening' spaces?) \$\endgroup\$
    – ATaco
    Sep 15, 2023 at 3:28
  • 2
    \$\begingroup\$ Could you please also clarify if not being comfortable is a condition for the bun to start running, a condition for it to continue running, or both? Also, does a bun with no escape squares presumably stay still? \$\endgroup\$
    – xnor
    Sep 15, 2023 at 5:25

1 Answer 1

3
\$\begingroup\$

Python3, 791 bytes:

M=[(0,1),(0,-1),(1,0),(-1,0)]
def F(b,x,y,X,Y,s):
 q,s=[(X,Y)],s+[(X,Y)]
 while q:
  X,Y=q.pop(0)
  for J,K in M:
   if(U:=(X+J,Y+K))not in s and' '==b[X+J][Y+K]:yield(*U,(J,K));q+=[U];s+=[U]
def f(b,c,C):
 (y,x),(Y,X)=c,C
 q=[(X,Y,*([(0,-1),(0,1)][y<Y]if x==X else[(-1,0),(1,0)][x<X]),[])]
 while q:
  X,Y,j,k,s=q.pop(0);P=abs(x-X)+abs(y-Y)>2
  T=[((J,K),any(B!=(J,K)for B in F(b,x,y,X+J,Y+K,[(X,Y)])))for J,K in M if(X+J,Y+K)not in s and' '==b[X+J][Y+K] and abs(x-X)+abs(y-Y)<abs(x-X-J)+abs(y-Y-K)]
  T=[(a,b)for a,b in T if b or not any(B for _,B in T)]
  if[]==T or (len(T)==1 and not T[0][1]and P):return(X,Y)
  if any(i[0]!=(j,k)for i in T)and P:return(X,Y)
  if(K:=[(0,j),(-1*k,0)][k!=0])in[i[0]for i in T]and P:return(X+K[0],Y+K[1])
  for(J,K),_ in T:q+=[(X+J,Y+K,J,K,s+[(X+J,Y+K)])]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save 5 bytes from indentation, by condensing variable declarations, lines 9-10 can be made into one, and 12-14 can be condensed. Eg. T=... and T=... can be T=...;T=... \$\endgroup\$ Sep 23, 2023 at 6:52

Your Answer

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

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