-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21-1.groovy
57 lines (52 loc) · 1.31 KB
/
day21-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
def sampleInput = '''...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........'''.split('\n') as List<String>
def input = new File('inputs/day21-1.txt').readLines()
def neighbours(pos, map) {
def dirs = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0]
]
dirs.collect { dir ->
[pos[0] + dir[0], pos[1] + dir[1]]
}.findAll { newPos ->
newPos[0] >= 0 && newPos[0] < map.size() && newPos[1] >= 0 && newPos[1] < map[newPos[0]].size() && map[newPos[0]][newPos[1]] != '#'
}
}
def bfsControlled(map, startPos, steps) {
def currentLayer = [startPos] as Set
for(int i = 0; i < steps; i++) {
def nextLayer = new HashSet()
currentLayer.each { pos ->
nextLayer.addAll(neighbours(pos, map))
}
currentLayer = nextLayer
}
println currentLayer
currentLayer.size()
}
def solve(List<String> map, int steps = 64) {
def pos = []
map.eachWithIndex { row, i ->
row.eachWithIndex{ String sign, j ->
if(sign == 'S') {
pos = [i, j]
return
}
}
if(pos != []) return
}
bfsControlled(map, pos, steps)
}
assert 16 == solve(sampleInput, 6)
println solve(input)