Skip to main content
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
  k | k%27 | dx=k%27%4 | k%5 | dy=k%5%3 | (dx, dy)          | 0 1 2
----+------+-----------+-----+----------+--------------  ---+-------
  6 |   6  |     2     |  1  |     1    | (+2, +1) (A)    0 | - C -
 52 |  25  |     1     |  2  |     2    | (+1, +2) (B)    1 | E D A
 98 |  17  |     1     |  3  |     0    | (+1, +0) (C)    2 | - B -
144 |   9  |     1     |  4  |     1    | (+1, +1) (D)
190 |   1  |     1     |  0  |     0    | (+1, +0) (C)
236 |  20  |     0     |  1  |     1    | (+0, +1) (E)
  k | k%27 | dx=k%27%4 | k%5 | dy=k%5%3 | (dx, dy) 
----+------+-----------+-----+----------+----------
  6 |   6  |     2     |  1  |     1    | (+2, +1)
 52 |  25  |     1     |  2  |     2    | (+1, +2)
 98 |  17  |     1     |  3  |     0    | (+1, +0)
144 |   9  |     1     |  4  |     1    | (+1, +1)
190 |   1  |     1     |  0  |     0    | (+1, +0)
236 |  20  |     0     |  1  |     1    | (+0, +1)
  k | k%27 | dx=k%27%4 | k%5 | dy=k%5%3 | (dx, dy)          | 0 1 2
----+------+-----------+-----+----------+--------------  ---+-------
  6 |   6  |     2     |  1  |     1    | (+2, +1) (A)    0 | - C -
 52 |  25  |     1     |  2  |     2    | (+1, +2) (B)    1 | E D A
 98 |  17  |     1     |  3  |     0    | (+1, +0) (C)    2 | - B -
144 |   9  |     1     |  4  |     1    | (+1, +1) (D)
190 |   1  |     1     |  0  |     0    | (+1, +0) (C)
236 |  20  |     0     |  1  |     1    | (+0, +1) (E)
merged the new version into the main one
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

NB: Among many different possible choices, the initial value of \$k\$ in \$g\$ was forced to \$6\$ so that it allows us to do g(0, Y = 6) in the main function without breaking anything.

NB: Among many different possible choices, the initial value of \$k\$ in \$g\$ was forced to \$6\$ so that it allows us to do g(0, Y = 6) in the main function without breaking anything.

merged the new version into the main one
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES6), 137 bytes

This one uses a more convoluted version of the helper function \$g\$.

Explanation to be updated.

m=>m.map((r,y)=>r.map((c,x)=>t+=c+=(g=(X,k=6)=>~k>0||(m[y+Y+k%5%3]||0)[x-X+k%53%3]&g(X,k*68))(Y=0)&&2+3*g(Y=3)*g(-3)*g``*g(0,Y=6)),t=0)|t

Try it online!


JavaScript (ES6),  146 ... 142140  140137 bytes

m=>m.map((r,y)=>r.map((c,x)=>t+=c+=(g=(X,kk=6)=>k>8||(325>>k|=>k>>8||(m[y+Y+k/3|0]||0m[y+Y+k%5%3]||0)[x-X+k%3])&gX+k%27%4]&g(X,-~kk+46))(Y=0)&&2+3*g(Y=3)*g(-3)*g``*g(!(0,Y=6))),t=0)|t

Try it online!Try it online!

How?

CommentedHelper function

The relative position in the submatrix isWe start with \$(dx,dy)=(k\bmod 3,\lfloor k/3\rfloor)\$\$k=6\$ and we test the least significant bit ofadd 325 >> k\$46\$ to figure out whether we're over\$k\$ after each iteration. The relative coordinates in the plus sign or notsubmatrix are given by:

0 1 2                         1 0 1
3 4 5  --- 325 >> k & 1 --->  0 0 0
6 7 8                         1 0 1

$$\begin{align}&dx=(k\bmod 27)\bmod 4\\ &dy=(k\bmod 5)\bmod 3\end{align}$$

  k | k%27 | dx=k%27%4 | k%5 | dy=k%5%3 | (dx, dy) 
----+------+-----------+-----+----------+----------
  6 |   6  |     2     |  1  |     1    | (+2, +1)
 52 |  25  |     1     |  2  |     2    | (+1, +2)
 98 |  17  |     1     |  3  |     0    | (+1, +0)
144 |   9  |     1     |  4  |     1    | (+1, +1)
190 |   1  |     1     |  0  |     0    | (+1, +0)
236 |  20  |     0     |  1  |     1    | (+0, +1)

During the first iterationThe cell at \$(+1, +0)\$ is tested twice, which is not an issue.

The next value of \$k\$ is actually undefined. But\$282\$ which triggers the test 325k >> undefined & 18 is \$1\$ as expected and the value ofstops the cell at this position is ignored anywayrecursion.

g = (X, k) =>  = 6) =>    // g is a recursive function taking X and thea counter k
  k >>> 8 || (         //   if k = 9282, stop the recursion and return 1
    325( >>m[ ky |+ Y +     //   otherwise, return 1 if we are outsidetest the '+'cell signlocated at
    ( m[y + Y + k % 5 % 3 ] //   or the cell located at row y + Y + floor((k /mod 35)
        k /mod 3 | 0]   //)
      || 0           //
    )[x[ x - X + k % 3]    //   and column x - X + ((k mod 327) ismod set4)
       k % 27 % 4 ]  //
  )                  //
  & g(X, -~k) k + 46)     //   do a recursive call with k + 146

Main function:

Main function

m =>                 // m[] = input matrix
m.map((r, y) =>      // for each row r[] at position y in m[]:
  r.map((c, x) =>    //   for each cell c at position x in r[]:
    t +=             //     add to t:
    c +=             //       1 point if c = 1
      g(Y = 0) && 2  //       2 points if there's a Double Plus at (x, y)
      + 3 *          //       3 points if there are also Double Pluses at:
      g(Y = 3) *     //         (x - 3, y + 3)
      g(-3) *        //         (x + 3, y + 3)
      g`` *          //         (x, y + 3)
      g(!(0, Y = 6))    //         (x, y + 6)
  ),                 //   end of inner map()
  t = 0              //   start with t = 0
) | t                // end of outer map(); return t

JavaScript (ES6), 137 bytes

This one uses a more convoluted version of the helper function \$g\$.

Explanation to be updated.

m=>m.map((r,y)=>r.map((c,x)=>t+=c+=(g=(X,k=6)=>~k>0||(m[y+Y+k%5%3]||0)[x-X+k%53%3]&g(X,k*68))(Y=0)&&2+3*g(Y=3)*g(-3)*g``*g(0,Y=6)),t=0)|t

Try it online!


JavaScript (ES6),  146 ... 142  140 bytes

m=>m.map((r,y)=>r.map((c,x)=>t+=c+=(g=(X,k)=>k>8||(325>>k|(m[y+Y+k/3|0]||0)[x-X+k%3])&g(X,-~k))(Y=0)&&2+3*g(Y=3)*g(-3)*g``*g(!(Y=6))),t=0)|t

Try it online!

Commented

The relative position in the submatrix is \$(dx,dy)=(k\bmod 3,\lfloor k/3\rfloor)\$ and we test the least significant bit of 325 >> k to figure out whether we're over the plus sign or not:

0 1 2                         1 0 1
3 4 5  --- 325 >> k & 1 --->  0 0 0
6 7 8                         1 0 1

During the first iteration, \$k\$ is actually undefined. But 325 >> undefined & 1 is \$1\$ as expected and the value of the cell at this position is ignored anyway.

g = (X, k) =>        // g is a recursive function taking X and the counter k
  k > 8 || (         //   if k = 9, stop the recursion and return 1
    325 >> k |       //   otherwise, return 1 if we are outside the '+' sign
    ( m[y + Y +      //   or the cell located at row y + Y + floor(k / 3)
        k / 3 | 0]   //
      || 0           //
    )[x - X + k % 3] //   and column x - X + (k mod 3) is set
  )                  //
  & g(X, -~k)        //   do a recursive call with k + 1

Main function:

m =>                 // m[] = input matrix
m.map((r, y) =>      // for each row r[] at position y in m[]:
  r.map((c, x) =>    //   for each cell c at position x in r[]:
    t +=             //     add to t:
    c +=             //       1 point if c = 1
      g(Y = 0) && 2  //       2 points if there's a Double Plus at (x, y)
      + 3 *          //       3 points if there are also Double Pluses at:
      g(Y = 3) *     //         (x - 3, y + 3)
      g(-3) *        //         (x + 3, y + 3)
      g`` *          //         (x, y + 3)
      g(!(Y = 6))    //         (x, y + 6)
  ),                 //   end of inner map()
  t = 0              //   start with t = 0
) | t                // end of outer map(); return t

JavaScript (ES6),  146 ... 140  137 bytes

m=>m.map((r,y)=>r.map((c,x)=>t+=c+=(g=(X,k=6)=>k>>8||(m[y+Y+k%5%3]||0)[x-X+k%27%4]&g(X,k+46))(Y=0)&&2+3*g(Y=3)*g(-3)*g``*g(0,Y=6)),t=0)|t

Try it online!

How?

Helper function

We start with \$k=6\$ and add \$46\$ to \$k\$ after each iteration. The relative coordinates in the submatrix are given by:

$$\begin{align}&dx=(k\bmod 27)\bmod 4\\ &dy=(k\bmod 5)\bmod 3\end{align}$$

  k | k%27 | dx=k%27%4 | k%5 | dy=k%5%3 | (dx, dy) 
----+------+-----------+-----+----------+----------
  6 |   6  |     2     |  1  |     1    | (+2, +1)
 52 |  25  |     1     |  2  |     2    | (+1, +2)
 98 |  17  |     1     |  3  |     0    | (+1, +0)
144 |   9  |     1     |  4  |     1    | (+1, +1)
190 |   1  |     1     |  0  |     0    | (+1, +0)
236 |  20  |     0     |  1  |     1    | (+0, +1)

The cell at \$(+1, +0)\$ is tested twice, which is not an issue.

The next value of \$k\$ is \$282\$ which triggers the test k >> 8 and stops the recursion.

g = (X, k = 6) =>    // g is a recursive function taking X and a counter k
  k >> 8 || (        //   if k = 282, stop the recursion and return 1
    ( m[ y + Y +     //   otherwise, test the cell located at
         k % 5 % 3 ] //   row y + Y + ((k mod 5) mod 3)
      || 0           //
    )[ x - X +       //   and column x - X + ((k mod 27) mod 4)
       k % 27 % 4 ]  //
  )                  //
  & g(X, k + 46)     //   do a recursive call with k + 46

Main function

m =>                 // m[] = input matrix
m.map((r, y) =>      // for each row r[] at position y in m[]:
  r.map((c, x) =>    //   for each cell c at position x in r[]:
    t +=             //     add to t:
    c +=             //       1 point if c = 1
      g(Y = 0) && 2  //       2 points if there's a Double Plus at (x, y)
      + 3 *          //       3 points if there are also Double Pluses at:
      g(Y = 3) *     //         (x - 3, y + 3)
      g(-3) *        //         (x + 3, y + 3)
      g`` *          //         (x, y + 3)
      g(0, Y = 6)    //         (x, y + 6)
  ),                 //   end of inner map()
  t = 0              //   start with t = 0
) | t                // end of outer map(); return t
added a shorter version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 2 bytes
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added more details about the helper function
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 1 byte
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 1 byte
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
fixed golfed code / commented code consistency
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 2 bytes
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added a commented version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading