Day 12: Garden Groups

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • @mykl
    link
    12 hours ago

    Uiua

    Takes about 3 seconds to solve both parts for live data, caused primarily by my terrible fill function in FieldCoords which repeatedly refills and dedups already discovered cells. I promised myself when I wrote it that I would revisit it, but I really can’t be bothered right now. Sorry Kai.

    Data   ← ⊜∘⊸≠@\n "AAAA\nBBCD\nBBCC\nEEEC"
    N₄     ← [¯1_0 1_0 0_¯1 0_1]               # Four orthogonal neighbours.
    Fences ← /+≡(/+=0⬚0⊡+N₄¤)⊙¤⊚.°⊚            # Fences for a field, by looking for edges.
    Cs     ← [0 1 1 0 1 0 2 1 1 2 0 1 0 1 1 0] # Number of corners keyed by bitarray of 2x2 grid.
    Sides  ← /+/+⧈(⊡:Cs°⋯♭)2_2⌝↘¯1_¯1⌝↘1_1°⊚   # Add border, look for corners in 2x2 windows.
    
    ValidN₄     ← ▽≠@_⬚@_⊡:Data.+N₄¤
    FieldCoords ← ⍥(◴⍆⊂▽⊙:=⊢∩(⊡:Data),,⟜(⍆◴∧(⊂ValidN₄)⊙[]))∞¤  # Terrible fill to get a list of coords given a starting point.
    Fields      ← ↘1{⍢(:⊙▽:¬∈,,FieldCoords⊢.|>0⧻)}⊙◌↯∞_2°⊡Data # Repeatedly find next fields coords until grid exhausted.
    
    /+×≡◇⊃⧻Fences Fields
    /+×≡◇⊃⧻Sides Fields