-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12_1.groovy
70 lines (64 loc) · 1.61 KB
/
day12_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
def sampleInput = '''RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE'''
static def inMap(def pos, List<String> map) {
pos[0] >= 0 && pos[0] < map.size() &&
pos[1] >= 0 && pos[1] < map[0].size()
}
static def getNeighbours(def pos) {
def DIRS = [[-1, 0], [0, -1], [1, 0], [0, 1]]
DIRS.collect {
[pos[0]+it[0], pos[1]+it[1]]
}
}
static def get(List<String> map, def pos) {
map[pos[0]][pos[1]]
}
static def bfs(def pos, List<String> map) {
def visited = [] as Set
def queue = [pos] as Queue
def area = 0
def fence = 0
visited.add(pos)
while (!queue.isEmpty()) {
def next = queue.poll()
area++
getNeighbours(next).each {
if (inMap(it, map)) {
if (visited.contains(it)) return
if (get(map, it) == get(map, pos)) {
visited.add(it)
queue.add(it)
} else {
fence++
}
} else {
fence++
}
}
}
[area, fence, visited]
}
static long solve(List<String> input) {
def visited = [] as Set
long cost = 0
input.eachWithIndex { line, i ->
line.eachWithIndex { it, j ->
if(!visited.contains([i, j])) {
def (area, fence, itemVisited) = bfs([i, j], input)
visited.addAll(itemVisited)
cost += area * fence
}
}
}
cost
}
assert 1930L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day12.txt').readLines())