12
\$\begingroup\$

Challenge

This coding challenge is to figure out how many rounds the cat can live.
In a \$4\times4\$ matrix, there are a number of mice and exactly 1 cat.
Example: $$ \begin{array} {|r|r|}\hline 🐭 & 🐭 & 🐭 & ⬜ \\ \hline ⬜ & 🐭 & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & 🐱⬜ & 🐭 \\ \hline 🐭 & 🐭 & 🐭 & 🐭 \\ \hline \end{array} $$

But in each square of the matrix, like a house, up to 5 mice can live in it.
I indicate it with a number in front of the mouse.
There are also squares where there are no mice => Indicated with a blank square. Example: $$ \begin{array} {|r|r|}\hline 1🐭 & 2🐭 & 3🐭 & ⬜ \\ \hline ⬜ & 5🐭 & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & 🐱⬜ & 2🐭 \\ \hline 1🐭 & 4🐭 & 1🐭 & 1🐭 \\ \hline \end{array} $$

About the Cat and Mouse

The cat can, and must, move up, down, left, right and diagonal, 1 step at a time. Take into note, that the cat can only eat 1 mouse per round. The cat will always eat a mouse, because it is always hungry.

The cat prefers the house with the most mice in it, although it knows it can eat just one at a time (don't ask me why).
After the cat has eaten a mouse, the number of mice in the house will of course decrease.
After the cat has eaten a mouse, the cat lives in the home of the eaten mouse, possibly with other mice for the remainder of the round.

In the starting position, the cat can only live where there is no mice. But even after the first round, of course the cat must live in a house of the mice.

This goes on and on, till:

Game End

These are the scenarios, when the game ends:

  • When there are no more mice around the cat to eat anymore.
    => The cat will starve. (Note the cat cannot eat another mouse in the current house since it must move on, so can end up starving while residing with mice - like in example 5)
  • When at least 2 of the houses, the cat can visit, has the highest and same number of mice.
    => The cat will die of frustration.

Rules

  • The Input must be a list, or an array, or some datatype that can store the number of mice in the house, and where the cat is.
  • Where there is no mice, you can indicate it with just \$0🐭\$
  • If you use an array, it could be 1 dimensional, but also 2 dimensional.
  • The output must be an integer, the number of rounds the cat did survive.
  • Standard Loopholes apply, of course.
  • This is , so the shortest code wins.

Good luck!

Note: In the above matrix I showed, the output must be \$3\$.
=> Death because: the cat can't decide in which house of mice to eat.

Example

Example 1

  • Starting state: $$ \begin{array} {|r|r|}\hline 1🐭 & 2🐭 & 3🐭 & ⬜ \\ \hline ⬜ & 5🐭 & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & 🐱⬜ & 2🐭 \\ \hline 1🐭 & 4🐭 & 1🐭 & 1🐭 \\ \hline \end{array} $$
  • After 1 round: $$ \begin{array} {|r|r|}\hline 1🐭 & 2🐭 & 3🐭 & ⬜ \\ \hline ⬜ & 4🐭\!\!\!\!🐱\!\!\!\! & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline 1🐭 & 4🐭 & 1🐭 & 1🐭 \\ \hline \end{array} $$
  • After 2 rounds: $$ \begin{array} {|r|r|}\hline 1🐭 & 2🐭 & 2🐭\!\!\!\!🐱\!\!\!\! & ⬜ \\ \hline ⬜ & 4🐭 & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline 1🐭 & 4🐭 & 1🐭 & 1🐭 \\ \hline \end{array} $$
  • After 3 rounds: $$ \begin{array} {|r|r|}\hline 1🐭 & 2🐭 & 2🐭 & ⬜ \\ \hline ⬜ & 3🐭\!\!\!\!🐱\!\!\!\! & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline 1🐭 & 4🐭 & 1🐭 & 1🐭 \\ \hline \end{array} $$
  • 4th Round: Death of frustration $$ \begin{array} {|r|r|}\hline 1🐭 & \underbrace{2🐭} & \underbrace{2🐭} & ⬜ \\ \hline ⬜ & 3🐭\!\!\!\!😿\!\!\!\! & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline 1🐭 & 4🐭 & 1🐭 & 1🐭 \\ \hline \end{array} $$

So it just survived 3 rounds.

Example 2

  • Starting Stage $$ \begin{array} {|r|r|}\hline 1🐭 & 5🐭 & 1🐭 & 🐱⬜ \\ \hline ⬜ & 5🐭 & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline ⬜ & ⬜ & ⬜ & 1🐭 \\ \hline \end{array} $$
  • End Stage: 1 Round $$ \begin{array} {|r|r|}\hline 1🐭 & 5🐭 & ⬜😿 & ⬜ \\ \hline ⬜ & 5🐭 & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline ⬜ & ⬜ & ⬜ & 1🐭 \\ \hline \end{array} $$

Example 3

  • Starting Stage $$ \begin{array} {|r|r|}\hline 1🐭 & 5🐭 & 1🐭 & ⬜ \\ \hline ⬜ & 5🐭 & ⬜ & ⬜ \\ \hline 2🐭 & 🐱⬜ & 1🐭 & 4🐭 \\ \hline ⬜ & ⬜ & 1🐭 & 1🐭 \\ \hline \end{array} $$
  • End Stage: 7 Rounds $$ \begin{array} {|r|r|}\hline 1🐭 & 2🐭 & 1🐭 & ⬜ \\ \hline ⬜ & 1🐭\!\!\!\!😿\!\!\!\! & ⬜ & ⬜ \\ \hline 2🐭 & ⬜ & 1🐭 & 4🐭 \\ \hline ⬜ & ⬜ & 1🐭 & 1🐭 \\ \hline \end{array} $$

Example 4

  • Starting Stage $$ \begin{array} {|r|r|}\hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline ⬜ & 1🐭 & ⬜ & ⬜ \\ \hline 🐱⬜ & ⬜ & 1🐭 & 1🐭 \\ \hline ⬜ & ⬜ & ⬜ & 2🐭 \\ \hline \end{array} $$
  • End Stage: 5 Rounds $$ \begin{array} {|r|r|}\hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & ⬜😿 \\ \hline \end{array} $$

Example 5

  • Starting Stage $$ \begin{array} {|r|r|}\hline ⬜ & 3🐭 & ⬜ & ⬜ \\ \hline ⬜ & 2🐭 & ⬜ & ⬜ \\ \hline 🐱⬜ & ⬜ & 1🐭 & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline \end{array} $$
  • End Stage: 4 Rounds $$ \begin{array} {|r|r|}\hline ⬜ & 1🐭\!\!\!\!😿\!\!\!\! & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline ⬜ & ⬜ & 1🐭 & ⬜ \\ \hline ⬜ & ⬜ & ⬜ & ⬜ \\ \hline \end{array} $$ Good luck again!
\$\endgroup\$
22
  • \$\begingroup\$ Thanks for using the sandbox, but just so you know, typically you should leave posts there for about two days in order to make sure you've really ironed out any potential issues \$\endgroup\$
    – pxeger
    Apr 6 at 8:50
  • 1
    \$\begingroup\$ @ManishKundu 1) No, the cat dies only when there are houses which it can reach has mice with the same number. 2) No, it's hungry always. 3) Yes it can only eat mice in the houses which is 1 step near. I'll include it in the post. Thanks. \$\endgroup\$
    – math
    Apr 6 at 8:55
  • 3
    \$\begingroup\$ Regarding sandboxing period, my rule of thumb is a week, plus explicitly asking for feedback in the chat a couple times. \$\endgroup\$
    – Bubbler
    Apr 6 at 8:57
  • 1
    \$\begingroup\$ @ManishKundu Ok, working on it. I'll add the starting stage and the end stage. \$\endgroup\$
    – math
    Apr 6 at 9:03
  • 1
    \$\begingroup\$ "(note that the cat." Hum OK, noted.. stares at the empty wall in confuse thoughts about a bracket that was never closed, a nightmare for all us coders - then looks at the cat sleeping on his lap Purr? Yes noted that too! ;) \$\endgroup\$
    – Kaddath
    Apr 6 at 16:26
3
\$\begingroup\$

JavaScript (ES7),  122 113 112  111 bytes

Saved 1 byte thanks to @l4m2

Expects (mice_array)(cat_x, cat_y).

m=>g=(x,y)=>!m.reduce((o,v,n)=>6>>(n%4-x)**2+((n>>2)-y)**2&1?v>h?!(p=n,h=v):o|v==h:o,h=0)&&1+g(p&3,p>>2,m[p]--)

Try it online!

Commented

This is a bit similar to my answer to my own challenge.

m =>                   // m[] = flat array of mice
g =                    // g is a recursive function taking
(x, y) =>              // the position (x, y) of the cat
!m.reduce((o, v, n) => // for each value v at position n in m[]:
  6 >>                 //   bitmask of valid squared distances
  (n % 4 - x) ** 2 +   //   compute (prev_x - x)²
  ((n >> 2) - y) ** 2  //   + (prev_y - y)²
  & 1 ?                //   if it's either 1 or 2:
    v > h ?            //     if v is greater than the highest value h:
      !(p = n, h = v)  //       copy n to p and v to h, clear o
    :                  //     else:
      o | v == h       //       set o if v = h (frustrated cat!)
  :                    //   else:
    o,                 //     leave o unchanged
  h = 0                //   start with o = h = 0
)                      // end of reduce()
&&                     // if it's falsy:
  1 +                  //   increment the final result
  g(                   //   and do a recursive call:
    p & 3,             //     new x
    p >> 2,            //     new y
    m[p]--             //     decrement m[p]
  )                    //   end of recursive call
\$\endgroup\$
8
  • \$\begingroup\$ Wow. looks good. \$\endgroup\$
    – math
    Apr 6 at 10:53
  • \$\begingroup\$ Seems you don't use that no valid move can also be treated as frustraction as every way is 0 \$\endgroup\$
    – l4m2
    Apr 6 at 11:44
  • 1
    \$\begingroup\$ I mean this \$\endgroup\$
    – l4m2
    Apr 6 at 11:50
  • \$\begingroup\$ It crashes for empty init board \$\endgroup\$
    – l4m2
    Apr 6 at 11:59
  • \$\begingroup\$ @l4m2 Yup. Rolled back. \$\endgroup\$
    – Arnauld
    Apr 6 at 12:06
3
\$\begingroup\$

Haskell, 137 134 bytes

(x,y)!l|d<-[a|a@((i,j),k)<-l,((x-i)^2+(y-j)^2-2)^2<2],m<-foldr(max.snd)1d,[i]<-[i|(i,k)<-d,k==m]=1+i!((i,m-1):[a|a<-l,fst a/=i])|0<1=0

Try it online!

The function (!) takes as input the position of the cat (x,y) and a list with one ((i,j),k) entry for each house, where house at position (i,j) as k mice (possibly zero).

\$\endgroup\$
3
\$\begingroup\$

Python 3.8, 167 \$\cdots\$ 159 155 bytes

Saved 2 5 bytes thanks to ovs!!!
Saved 4 bytes thanks to Delfad0r!!!

def f(a,c,r=0):
 while(l:=sorted([[z,-a.get(c+z,0)]*2for x in(1,1j,1+1j,1-1j)for z in(x,-x)],key=list.pop))[0][1]<l[1][1]:r+=1;c+=l[0][0];a[c]-=1
 return r

Try it online!

Inputs a dictionary mapping each house in the array (represented by a complex number) to the number of mice in that house along with the position of the cat as a complex number.
Return the number of rounds before the cat "dies" (no cats or mice where harmed in this answer, they're all professional actors just playing their parts).

\$\endgroup\$
6
  • \$\begingroup\$ -2 bytes by using list.pop as a key function. \$\endgroup\$
    – ovs
    Apr 6 at 20:37
  • \$\begingroup\$ @ovs Nice one, was wondering if there's a better key function - thanks! :D \$\endgroup\$
    – Noodle9
    Apr 6 at 20:46
  • \$\begingroup\$ And 161 bytes by enumerating all values of x+y. \$\endgroup\$
    – ovs
    Apr 6 at 20:51
  • \$\begingroup\$ @ovs Very nice - thanks! :D \$\endgroup\$
    – Noodle9
    Apr 6 at 20:59
  • 1
    \$\begingroup\$ @Delfad0r Sweet, was trying to reduce that huge tuple - thanks! :D \$\endgroup\$
    – Noodle9
    Apr 7 at 12:51
2
\$\begingroup\$

Charcoal, 39 bytes

E⁴SJNNW⁼№KM⌈KM¹«⊞υωM✳⁻³⌕KM⌈KMPI⊖KK»⎚ILυ

Try it online! Link is to verbose version of code. Takes as input a list of 4 strings of digits, followed by the 0-indexed across and down coordinates. Explanation:

E⁴S

Print the mouse counts to the canvas.

JNN

Move to the cat's starting position.

W⁼№KM⌈KM¹«

Repeat while the cat has a unique choice of best move.

⊞υω

Track the number of moves.

M✳⁻³⌕KM⌈KM

Actually make that move. (Annoyingly the Peek and the Direction directions don't match up with each other...)

PI⊖KK

Decrement the number of mice at the new position.

»⎚ILυ

Clear the canvas and output the number of moves taken.

\$\endgroup\$

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.