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.~~
LATE EDIT: Thanks to Quant for the inspiration to revisit this. With his code snippet and the realisation that I should normalise all fields to remove wasted space, runtime is now down to 55ms.
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 β /+ββ¬0β§(β‘:CsΒ°β―β)2_2Β°β # Add border, look for corners in 2x2 windows.
# Use `classify` to find fields, then normalise to 0_0.
Fields β β‘β(-Β€βΈ/β§)ββ‘:β‘β³.+1βββData # Thanks to Quant!
/+Γβ‘ββ⧻Fences Fields
/+Γβ‘ββ⧻Sides Fields