-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6_1.groovy
64 lines (54 loc) · 1.47 KB
/
day6_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
def sampleInput = '''....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...'''
enum DIR {
UP('^', [-1, 0]), RIGHT('>', [0, 1]), DOWN('v', [1, 0]), LEFT('<', [0, -1]), ;
def sign
def delta
DIR(sign, delta) {
this.sign = sign
this.delta = delta
}
}
static def next(List<String> input, def position) {
def dir = position[2]
def nextPos = [position[0]+dir.delta[0], position[1]+dir.delta[1], dir]
if(inBoard(input, nextPos) && input[nextPos[0]][nextPos[1]] == '#') {
nextPos = [position[0], position[1], DIR.values()[(dir.ordinal()+1)%4]]
}
nextPos
}
static def findStart(List<String> input) {
int n = input.size()
for (int i = 0; i < n; i++) {
def dirs = DIR.values();
for (int k = 0; k < dirs.length; k++) {
def j = input[i].indexOf(dirs[k].sign)
if (j >= 0) {
return [i, j, dirs[k]]
}
}
}
}
static boolean inBoard(List<String> board, def pos) {
pos[0] >= 0 && pos[0] < board.size() &&
pos[1] >= 0 && pos[1] < board[0].size()
}
static long solve(List<String> input) {
Set<List<Integer>> visited = new HashSet<>()
def pos = findStart(input)
while (inBoard(input, pos)) {
visited.add([pos[0], pos[1]])
pos = next(input, pos)
}
visited.size()
}
assert 41L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day6.txt').readLines())