summaryrefslogtreecommitdiff
path: root/solution.dyalog
blob: 7958b2812a9c2a825689e470354651b9152e5e42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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≡+/((⍵<mins),(⍵>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
}