-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16-1.groovy
85 lines (79 loc) · 2.53 KB
/
day16-1.groovy
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def sampleInput = '''.|...\\....
|.-.\\.....
.....|-...
........|.
..........
.........\\
..../.\\\\..
.-.-/..|..
.|....-|.\\
..//.|....'''.split('\n') as List<String>
def input = new File('inputs/day16-1.txt').readLines()
def inMap(N, M, pos) {
pos[0] >= 0 && pos[0] < N && pos[1] >= 0 && pos[1] < M
}
// 1 -> >
// 2 -> D
// 4 -> <
// 8 -> U
def next(pos, dir) {
switch(dir) {
case 1 -> [pos[0], pos[1]+1]
case 2 -> [pos[0]+1, pos[1]]
case 4 -> [pos[0], pos[1]-1]
case 8 -> [pos[0]-1, pos[1]]
default -> throw new IllegalStateException("Shouldnt' be here ${pos} ${dir}")
}
}
def dfs(List<String> map, byte[][] occupations, N, M) {
def stack = new Stack()
stack.push([[0,0], 1])
while(!stack.isEmpty()) {
def top = stack.pop()
def pos = top[0]
def dir = top[1]
if(!inMap(N, M, pos) || occupations[pos[0]][pos[1]] & dir) continue
occupations[pos[0]][pos[1]] |= dir
def el = map[pos[0]][pos[1]]
if(el == '.' || el == '|' && (dir == 2 || dir == 8) || el == '-' && (dir == 1 || dir == 4)) {
stack.push([next(pos, dir), dir])
} else if(el == '|') {
stack.push([next(pos, 2), 2])
stack.push([next(pos, 8), 8])
} else if(el == '-') {
stack.push([next(pos, 1), 1])
stack.push([next(pos, 4), 4])
} else if(el == '/') {
switch (dir) {
case 1 -> stack.push([next(pos, 8), 8])
case 2 -> stack.push([next(pos, 4), 4])
case 4 -> stack.push([next(pos, 2), 2])
case 8 -> stack.push([next(pos, 1), 1])
default -> throw new IllegalStateException("Shouldnt' be here ${pos} ${dir}")
}
} else if(el == '\\') {
switch (dir) {
case 1 -> stack.push([next(pos, 2), 2])
case 2 -> stack.push([next(pos, 1), 1])
case 4 -> stack.push([next(pos, 8), 8])
case 8 -> stack.push([next(pos, 4), 4])
default -> throw new IllegalStateException("Shouldnt' be here ${pos} ${dir}")
}
} else {
throw new IllegalStateException("Shouldnt' be here ${pos} ${dir}")
}
}
}
def solve(List<String> map) {
def N = map.size()
def M = map.get(0).size()
def occupations = new byte[N][M]
dfs(map, occupations, N, M)
(0..<N).sum { i ->
(0..<M).sum { j ->
occupations[i][j] ? 1 : 0
}
}
}
assert 46 == solve(sampleInput)
println solve(input)