Skip to main content
Bounty Ended with 200 reputation awarded by Adám
-4 bytes
Source Link
fireflame241
  • 16.3k
  • 2
  • 29
  • 72

APL (Dyalog Unicode), 7070 66 bytes

D←⊢-1∘⌽
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽ר(⍉1↓⌽)⍣4×(⊢-{⊂(,⍵)[6 9 8 7 4[8(-,⊢)3,⍳3]}⌺3 3)⎕

-4 bytes thanks to @Bubbler and @ngn

Full program that requires ⎕IO←0

⍝ Helper function D: cyclic differences of a list
D←⊢-1∘⌽
  ⊢     ⍝ Each element
   -    ⍝ Subtract
    1∘⌽ ⍝ The one after
⍝ Main code
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂(,⍵)[6 9 8 7 4,⍳3]}⌺3 3)⎕
⎕           ⍝ The input grid
{...}⌺3 3   ⍝ For the 3×3 window centered on each cell:
 (,⍵)[6 9 8 7 4[8(-,⊢)3,⍳3] ⍝ Obtain the cycle elements clockwise from the right
 ⊂                  ⍝ Ensure that the output is the same shape as G
×⊢-         ⍝ The sign of the difference between each cycle and its center in G
¯2(⍉1↓⌽)⍣4 ¯2↓1⊖1⌽   ⍝ Trim off the entries with centers on an edge/corner
{...}¨      ⍝ For each sign-difference cycle:
 ×⍵×D⍵      ⍝ The indices where a +/- segment ends
              ⍝ (nonzero and different from the following element)
 ⍵/⍨        ⍝ The signs of each segment in order
              ⍝ (must be ¯1 1 ¯1 1 or reverse to be truthy --- can only have entries +/- 1)
 D          ⍝ Cyclic differences (must be 2 ¯2 2 ¯2 or reverse to be truthy --- can only have entries ¯2, 0, or 2)
 16=×/      ⍝ Test if product is 16: ensures right length and right values.
            ⍝ Note that the sum of the cyclic differences are always 0, so 2 2 2 2 is impossible)
+/,         ⍝ Count the total number that meet the condition

APL (Dyalog Unicode), 70 bytes

D←⊢-1∘⌽
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂(,⍵)[6 9 8 7 4,⍳3]}⌺3 3)⎕
⍝ Helper function D: cyclic differences of a list
D←⊢-1∘⌽
  ⊢     ⍝ Each element
   -    ⍝ Subtract
    1∘⌽ ⍝ The one after
⍝ Main code
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂(,⍵)[6 9 8 7 4,⍳3]}⌺3 3)⎕
⎕           ⍝ The input grid
{...}⌺3 3   ⍝ For the 3×3 window centered on each cell:
 (,⍵)[6 9 8 7 4,⍳3] ⍝ Obtain the cycle elements clockwise from the right
 ⊂                  ⍝ Ensure that the output is the same shape as G
×⊢-         ⍝ The sign of the difference between each cycle and its center in G
¯2 ¯2↓1⊖1⌽  ⍝ Trim off the entries with centers on an edge/corner
{...}¨      ⍝ For each sign-difference cycle:
 ×⍵×D⍵      ⍝ The indices where a +/- segment ends
              ⍝ (nonzero and different from the following element)
 ⍵/⍨        ⍝ The signs of each segment in order
              ⍝ (must be ¯1 1 ¯1 1 or reverse to be truthy --- can only have entries +/- 1)
 D          ⍝ Cyclic differences (must be 2 ¯2 2 ¯2 or reverse to be truthy --- can only have entries ¯2, 0, or 2)
 16=×/      ⍝ Test if product is 16: ensures right length and right values.
            ⍝ Note that the sum of the cyclic differences are always 0, so 2 2 2 2 is impossible)
+/,         ⍝ Count the total number that meet the condition

APL (Dyalog Unicode), 70 66 bytes

D←⊢-1∘⌽
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨(⍉1↓⌽)⍣4×(⊢-{⊂(,⍵)[8(-,⊢)3,⍳3]}⌺3 3)⎕

-4 bytes thanks to @Bubbler and @ngn

Full program that requires ⎕IO←0

⍝ Helper function D: cyclic differences of a list
D←⊢-1∘⌽
  ⊢     ⍝ Each element
   -    ⍝ Subtract
    1∘⌽ ⍝ The one after
⍝ Main code
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂(,⍵)[6 9 8 7 4,⍳3]}⌺3 3)⎕
⎕           ⍝ The input grid
{...}⌺3 3   ⍝ For the 3×3 window centered on each cell:
 (,⍵)[8(-,⊢)3,⍳3] ⍝ Obtain the cycle elements clockwise from the right
 ⊂                ⍝ Ensure that the output is the same shape as G
×⊢-         ⍝ The sign of the difference between each cycle and its center in G
(⍉1↓⌽)⍣4    ⍝ Trim off the entries with centers on an edge/corner
{...}¨      ⍝ For each sign-difference cycle:
 ×⍵×D⍵      ⍝ The indices where a +/- segment ends
              ⍝ (nonzero and different from the following element)
 ⍵/⍨        ⍝ The signs of each segment in order
              ⍝ (must be ¯1 1 ¯1 1 or reverse to be truthy --- can only have entries +/- 1)
 D          ⍝ Cyclic differences (must be 2 ¯2 2 ¯2 or reverse to be truthy --- can only have entries ¯2, 0, or 2)
 16=×/      ⍝ Test if product is 16: ensures right length and right values.
            ⍝ Note that the sum of the cyclic differences are always 0, so 2 2 2 2 is impossible)
+/,         ⍝ Count the total number that meet the condition
Source Link
fireflame241
  • 16.3k
  • 2
  • 29
  • 72

APL (Dyalog Unicode), 70 bytes

D←⊢-1∘⌽
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂(,⍵)[6 9 8 7 4,⍳3]}⌺3 3)⎕

Try it online!

⍝ Helper function D: cyclic differences of a list
D←⊢-1∘⌽
  ⊢     ⍝ Each element
   -    ⍝ Subtract
    1∘⌽ ⍝ The one after
⍝ Main code
+/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂(,⍵)[6 9 8 7 4,⍳3]}⌺3 3)⎕
⎕           ⍝ The input grid
{...}⌺3 3   ⍝ For the 3×3 window centered on each cell:
 (,⍵)[6 9 8 7 4,⍳3] ⍝ Obtain the cycle elements clockwise from the right
 ⊂                  ⍝ Ensure that the output is the same shape as G
×⊢-         ⍝ The sign of the difference between each cycle and its center in G
¯2 ¯2↓1⊖1⌽  ⍝ Trim off the entries with centers on an edge/corner
{...}¨      ⍝ For each sign-difference cycle:
 ×⍵×D⍵      ⍝ The indices where a +/- segment ends
              ⍝ (nonzero and different from the following element)
 ⍵/⍨        ⍝ The signs of each segment in order
              ⍝ (must be ¯1 1 ¯1 1 or reverse to be truthy --- can only have entries +/- 1)
 D          ⍝ Cyclic differences (must be 2 ¯2 2 ¯2 or reverse to be truthy --- can only have entries ¯2, 0, or 2)
 16=×/      ⍝ Test if product is 16: ensures right length and right values.
            ⍝ Note that the sum of the cyclic differences are always 0, so 2 2 2 2 is impossible)
+/,         ⍝ Count the total number that meet the condition

A creative idea I had was taking 2×2 stencils of 2×2 stencils with ({⊂⍵}⌺2 2)⍣2⊢G then extracting cycles with {1⌷⊃,/0 1 2 3{⍉∘⌽⍣⍺⊢⍵}¨,⍵}¨. This might have been shorter (if golfed a bit more) because it didn't initially require ¯2 ¯2↓1⊖1, but unfortunately we still had to subtract corresponding elements of G.