Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

The Game of Cat And Mice | With Matrices

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!

Answer*

Draft saved
Draft discarded
Cancel

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /