Skip to main content
3 of 4
added 157 characters in body
user
  • 387
  • 2
  • 21
  • 68

APL (Dyalog Unicode), 35 33 bytes

Saved 2 bytes thanks to Adám (and would have saved 1 with a tip from Bubbler)

{1≥|⊃⍵-⍵⌹=⍨⍵:0⋄1+∇h,3÷⍨3+/⍵,h←⊃⍵}

Try it online!

A recursive function.

Explanation:

{
  1≥|⊃⍵-(+/÷≢)⍵: 0        ⍝ Base case: If it's the last iteration, return 0
         ⍵⌹=⍨⍵           ⍝ Average of ⍵ (the array) (not sure why that works)
            =⍨⍵           ⍝ Compare ⍵ to itself to create an array of 1s the same size as ⍵
         ⍵⌹               ⍝ ⍵ divided by that (matrix division)
       ⍵-                  ⍝ Subtract that from all elements of ⍵
      ⊃                    ⍝ Take only the first of those differences
     |                     ⍝ Absolute value
   1≥                      ⍝ Is it less than or equal to 1?
⋄1+∇h,3÷⍨3+/⍵,h←⊃⍵
               h←⊃⍵        ⍝ Assign the first element of ⍵ to h
            ⍵,             ⍝ Append to ⍵ (because of wrapping)
         3+/               ⍝ Take groups of 3 adjacent elements and sum each
      3÷⍨                  ⍝ Divide each sum by 3 (to get average)
    h,                     ⍝ Prepend h, which stays constant
   ∇                       ⍝ Call on this new iteration
 1+                        ⍝ Add 1 to that
}
user
  • 387
  • 2
  • 21
  • 68