summaryrefslogtreecommitdiff
path: root/solution.dyalog
blob: a915386541ed8b3db212326e035ae97d639a7c81 (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
49
⎕IO←0
]box on

⍝ Part 1
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                 ⍝ 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                              ⍝ 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{
		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
}