From 8b9f68dc46ba619a94686002805766618f214e5a Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 27 Jun 2024 16:13:02 +0200 Subject: Code --- solution.dyalog | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 solution.dyalog (limited to 'solution.dyalog') diff --git a/solution.dyalog b/solution.dyalog new file mode 100644 index 0000000..7958b28 --- /dev/null +++ b/solution.dyalog @@ -0,0 +1,48 @@ +⎕IO←0 +]box on + +⍝ Part 1 +cubes←⍎¨⊃⎕NGET'input'1 +movements←{⍵,-⍵}↓(3 3⍴4↑1) +sides←,cubes∘.+movements +≢sides~cubes + +⍝ Part 2 +mins←(↑⌊/cubes)-1 1 1 +maxs←(↑⌈/cubes)+1 1 1 +in_boundary←{0≡+/((⍵maxs))} +count←0 +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)~⍺) +}↓maxs + +⍝ search visualization +_←⎕ED 'grid' +visualize←{ + grid⊢←(maxs+1 1 1)⍴' ' + cubes{ ⍝ https://dfns.dyalog.com/n_bfs.htm + 0=≢⍵:count + head←1↑⍵ ⋄ tail←1↓⍵ + grid[|head]⊢←'O' + _←⎕DL .001 + grid[|head]⊢←'X' + neighbors←movements+¨head + next←{(in_boundary¨⍵)/⍵}neighbors + count⊢←count+(+/{(↓⍵)∊cubes}¨neighbors) + (⍺,head)∇((tail∪next)~⍺) + }↓maxs +} + +⍝ layer visualization +_←⎕ED 'layergrid' +render←{ + { + layergrid⊢←⍵ + _←⎕DL 0.3 + }¨↓↓grid +} -- cgit v1.2.3