diff options
Diffstat (limited to 'solution.dyalog')
-rw-r--r-- | solution.dyalog | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/solution.dyalog b/solution.dyalog index 7958b28..a915386 100644 --- a/solution.dyalog +++ b/solution.dyalog @@ -2,30 +2,31 @@ ]box on ⍝ Part 1 -cubes←⍎¨⊃⎕NGET'input'1 -movements←{⍵,-⍵}↓(3 3⍴4↑1) -sides←,cubes∘.+movements -≢sides~cubes +cubes←⍎¨⊃⎕NGET'../input'1 ⍝ All lava cubes +movements←{⍵,-⍵}↓(3 3⍴4↑1) ⍝ 6 possible movements each +sides←,cubes∘.+movements ⍝ Sides: Cubes added to movements +≢sides~cubes ⍝ Count of surface area ⍝ Part 2 -mins←(↑⌊/cubes)-1 1 1 -maxs←(↑⌈/cubes)+1 1 1 -in_boundary←{0≡+/((⍵<mins),(⍵>maxs))} -count←0 +mins←(↑⌊/cubes)-1 1 1 ⍝ Minimum coords (minx, miny, minz) +maxs←(↑⌈/cubes)+1 1 1 ⍝ Maximum coords (maxx, maxy, maxz) +in_boundary←{0≡+/((⍵<mins),(⍵>maxs))} ⍝ Is cube within boundary? +count←0 ⍝ Final count of outer surface area + cubes{ ⍝ https://dfns.dyalog.com/n_bfs.htm - 0=≢⍵:count - head←1↑⍵ ⋄ tail←1↓⍵ - neighbors←movements+¨head - next←{(in_boundary¨⍵)/⍵}neighbors - count⊢←count+(+/{(↓⍵)∊cubes}¨neighbors) - (⍺,head)∇((tail∪next)~⍺) + 0=≢⍵:count ⍝ If queue empty, return count + head←1↑⍵ ⋄ tail←1↓⍵ ⍝ head, tail of queue + neighbors←movements+¨head ⍝ 6 neighbors: head+movements + next←{(in_boundary¨⍵)/⍵}neighbors ⍝ Filter neighbors by in_boundary + count⊢←count+(+/{(↓⍵)∊cubes}¨neighbors) ⍝ Increment count with neighbors that are in cubes + (⍺,head)∇((tail∪next)~⍺) ⍝ Recursion: (visited|head) rec (tail|next)-visited }↓maxs ⍝ search visualization _←⎕ED 'grid' visualize←{ grid⊢←(maxs+1 1 1)⍴' ' - cubes{ ⍝ https://dfns.dyalog.com/n_bfs.htm + cubes{ 0=≢⍵:count head←1↑⍵ ⋄ tail←1↓⍵ grid[|head]⊢←'O' |