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

Return to Answer

added the 5th test case
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES7), (削除) 122 113 112 (削除ここまで) 112111 bytes

Saved 1 byte thanks to @l4m2

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/ho|v==h:o,h=0)*h&&1+g&&1+g(p&3,p>>2,m[p]--)

Try it online! Try it online!

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)2
 ((n >> 2) - y) ** 2 // + (prev_y - y)2
 & 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()
*&& h && // if it's falsy and h is not 0:
 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

JavaScript (ES7), (削除) 122 113 (削除ここまで) 112 bytes

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)*h&&1+g(p&3,p>>2,m[p]--)

Try it online!

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)2
 ((n >> 2) - y) ** 2 // + (prev_y - y)2
 & 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()
* h && // if it's falsy and h is not 0:
 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

JavaScript (ES7), (削除) 122 113 112 (削除ここまで) 111 bytes

Saved 1 byte thanks to @l4m2

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!

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)2
 ((n >> 2) - y) ** 2 // + (prev_y - y)2
 & 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
fixed version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
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)2
 ((n >> 2) - y) ** 2 // + (prev_y - y)2
 & 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()
* h && // if it's falsy and h is not 0:
 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
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)2
 ((n >> 2) - y) ** 2 // + (prev_y - y)2
 & 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
 : // else:
 o, // leave o unchanged
 h = 0 // start with o = h = 0
) // end of reduce()
* h && // if it's falsy and h is not 0:
 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
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)2
 ((n >> 2) - y) ** 2 // + (prev_y - y)2
 & 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()
* h && // if it's falsy and h is not 0:
 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
fixed version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 2 bytes and added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 9 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added 5 characters in body
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /