-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18_1.groovy
121 lines (111 loc) · 6.57 KB
/
day18_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def inputs = [
"[[[7,1],2],3]", "[[1,7],7]", "[[6,8],[[6,[3,6]],[0,5]]]", "[[[[2,1],8],[[9,4],8]],[[6,5],4]]", "[[1,[[3,8],[9,1]]],[[9,1],[[1,7],0]]]", "[[[7,4],[8,[7,6]]],[9,[[6,3],[7,8]]]]", "[[[[5,0],1],4],[[5,[6,9]],[[4,3],2]]]", "[[[3,8],8],[[[3,2],8],[9,[0,5]]]]", "[[[[5,8],[3,9]],[7,[1,4]]],[6,1]]", "[3,[[[3,3],9],[0,7]]]", "[[[6,9],1],[[0,[8,4]],[[2,2],9]]]", "[[[[6,2],3],[0,4]],3]", "[[[[3,8],7],[[7,4],0]],[2,[5,[2,8]]]]", "[[4,[9,[8,0]]],[[1,5],[[9,3],8]]]", "[[[8,5],[3,[1,4]]],[[6,[8,0]],[[2,7],[2,6]]]]", "[4,7]", "[[[[2,3],0],[[1,9],[4,1]]],[[1,[4,2]],3]]", "[[[8,[5,3]],[[5,7],7]],[[5,6],[6,4]]]", "[[[[2,4],1],[8,6]],[[6,5],[0,[9,1]]]]", "[[[1,[5,7]],8],[[[9,1],9],[[1,2],4]]]", "[[[[5,5],[4,0]],[4,[9,6]]],[[[2,1],1],7]]", "[[[[1,9],[9,5]],[[5,0],[3,1]]],[[[6,7],[8,8]],[[7,3],0]]]", "[[6,[[6,7],[9,0]]],[[7,7],[[0,3],0]]]", "[[0,6],[5,2]]", "[[[[5,8],3],[[9,0],8]],[7,4]]", "[[0,[[9,9],[9,4]]],[[[1,1],2],[1,[6,7]]]]", "[0,[[5,7],2]]", "[[2,[[5,4],6]],[1,[8,[7,6]]]]", "[[[1,7],[8,[5,8]]],[[[2,1],[9,1]],[[5,6],9]]]", "[[1,8],[9,[4,3]]]", "[5,[2,[[5,5],9]]]", "[3,[8,[[2,8],[4,8]]]]", "[[[4,9],[[5,5],0]],[9,[8,[3,0]]]]", "[[[2,[6,4]],[8,[9,9]]],[[[0,4],8],[3,[9,7]]]]", "[[[[8,1],[2,4]],3],[1,[[3,3],[6,3]]]]", "[[[8,[7,3]],[1,8]],2]", "[[8,[8,4]],[[6,[4,7]],[3,0]]]", "[[[[4,6],[8,3]],9],[9,[[8,9],[0,9]]]]", "[[3,[[2,7],[4,4]]],2]", "[8,[[[8,6],2],[[8,9],6]]]", "[[[[5,7],[2,0]],[[0,2],[5,5]]],[[[8,5],5],[[1,3],[2,3]]]]", "[[1,6],[[9,8],[9,[4,9]]]]", "[[[[1,4],5],9],[4,[6,8]]]", "[[[[6,4],[9,0]],[[1,4],[6,6]]],[[9,[2,8]],2]]", "[[[[5,9],2],[[0,0],5]],[2,1]]", "[6,[[3,2],[[3,0],0]]]", "[[[[7,4],1],[[4,1],1]],[[3,4],4]]", "[3,[9,[9,7]]]", "[[[3,[3,3]],[0,3]],[1,[1,8]]]", "[[8,[8,7]],[[9,2],5]]", "[[[1,[3,9]],[5,9]],[1,5]]", "[[[[7,8],[9,7]],9],[[[9,2],[2,2]],[[9,6],8]]]", "[4,[[3,5],[[1,3],[5,5]]]]", "[7,[[[0,1],2],[[3,6],5]]]", "[0,[[[2,4],[3,4]],[8,9]]]", "[[1,[[6,8],1]],[8,0]]", "[1,1]", "[7,0]", "[[1,2],[[0,[8,3]],[[4,5],[9,7]]]]", "[[[[2,3],[5,9]],[7,[1,9]]],2]", "[[3,5],[[9,7],9]]", "[[[[6,9],[4,8]],6],0]", "[[[[2,4],[3,9]],[2,[9,4]]],[[[8,9],[3,1]],7]]", "[[5,[[0,2],4]],[[[9,9],[7,4]],[1,5]]]", "[3,[6,[[5,4],1]]]", "[[[2,[2,7]],2],[[4,[7,3]],5]]", "[7,[[0,[2,0]],[[9,4],6]]]", "[[4,[3,[6,2]]],9]", "[[[0,[5,6]],[8,3]],[[7,9],[0,[9,6]]]]", "[8,[[6,4],[4,8]]]", "[[[8,[6,8]],[5,[7,3]]],[[[7,8],5],2]]", "[[[[3,5],[4,7]],5],[[0,0],[9,[1,9]]]]", "[[7,[[1,5],9]],[[[3,4],[1,7]],[1,[7,9]]]]", "[[0,[3,[4,1]]],[[[2,9],3],[4,[0,8]]]]", "[[[8,[1,6]],[[0,1],7]],[[[1,1],[0,2]],[[9,4],[9,6]]]]", "[[[[6,7],0],[[6,8],9]],[[1,[6,6]],[[2,9],[4,7]]]]", "[[[[5,0],[1,2]],[1,[5,1]]],[[0,4],1]]", "[[9,1],6]", "[[7,2],[[[5,5],[4,3]],6]]", "[[9,[[0,6],9]],[[7,9],[7,1]]]", "[[[[7,3],[6,4]],[[2,5],[7,2]]],[[[4,4],0],[[9,5],[8,5]]]]", "[[[[8,8],[6,4]],[[0,2],[9,5]]],2]", "[[[[3,0],7],[9,2]],[[0,[8,6]],[[7,2],[8,5]]]]", "[[0,6],[1,[9,[4,3]]]]", "[[0,8],[[[5,0],6],[5,[2,0]]]]", "[[[[7,1],[0,3]],[[9,9],[3,5]]],[4,[8,4]]]", "[7,[[1,[3,7]],[[3,4],[2,3]]]]", "[[[[2,2],[4,8]],[[3,4],0]],[[[1,5],[2,8]],5]]", "[6,[[[9,1],5],[9,9]]]", "[[[2,[8,6]],[[9,9],[6,3]]],4]", "[[[[3,2],[9,3]],8],9]", "[[[[6,9],0],[[0,6],[1,3]]],[[5,[9,8]],[[1,5],[3,7]]]]", "[[2,[4,[2,3]]],[[[6,0],[7,2]],3]]", "[[[[8,3],4],[6,[8,8]]],4]", "[[[9,8],5],[[[4,4],[6,3]],[8,6]]]", "[9,2]", "[[[3,4],[4,[7,0]]],[0,[4,[6,9]]]]", "[[[0,8],[3,9]],[[[3,8],6],[[9,3],6]]]", "[[[[5,6],[0,3]],1],[8,[2,9]]]", "[[[[4,2],8],[[9,3],7]],0]",
]
fishes = ''
inputs.eachWithIndex { input, ii ->
if (!fishes) {
fishes = input
return
}
fishes = "[${fishes},${input}]"
while(reduce('explode') || reduce('split')) {}
}
println(fishes)
cal(fishes)
def cal(toCal) {
def ni = -1
def nMap = [:]
def nodes = []
toCal.each { c ->
if (c == '[') {
ni++
nMap[ni] = []
nodes << ni
} else if (c == ']') {
def lastNode = nodes.last()
def number = 3 * nMap[lastNode][0] + 2 * nMap[lastNode][1]
nodes.remove(nodes.size()-1)
if (nodes.size() > 0) {
nMap[nodes.last()] << number
}
} else if (isNumber(c)) {
nMap[nodes.last()] << (c as Integer)
}
}
return 3 * nMap[0][0] + 2 * nMap[0][1]
}
def reduce(mode) {
def squares = 0
def reduced = false
fishes.eachWithIndex { c, ci ->
if (reduced) {
return
}
if (mode == 'explode') {
if (c == '[') {
if (squares >= 4) {
explode(ci)
reduced = true
}
squares++
} else if (c == ']') {
squares--
}
} else if (isNumber(c) && isNumber(fishes[ci+1])) {
split(ci)
reduced = true
}
}
return reduced
}
def isNumber(c) {
return c >= '0' && c <= '9'
}
def split(ci) {
def notNumber = fishes[ci..fishes.size()-1].findIndexOf { it < '0' || it > '9' }
def number = fishes[ci..ci+notNumber-1] as Integer
def pair = "[${(number/2) as Integer},${(Math.ceil(number/2) as Integer)}]"
fishes = "${fishes[0..ci-1]}${pair}${fishes[ci+notNumber..fishes.size()-1]}"
}
def explode(i) {
def li = fishes[i..(fishes.size()-1)].findIndexOf { it == ']' }
def numbers = fishes[i+1..i+li-1].split(',').collect { it as Integer }
fishes = "${fishes[0..i-1]}0${fishes[(i+li+1)..(fishes.size()-1)]}"
// println(fishes)
def findRight = false
(i+1..fishes.size()-1).each { it ->
if (findRight) {
return
}
if (fishes[it] >= '0' && fishes[it] <= '9') {
def notNumber = fishes[it..fishes.size()-1].findIndexOf { it < '0' || it > '9' }
def number = fishes[it..it+notNumber-1] as Integer
fishes = "${fishes[0..it-1]}${number+numbers[1]}${fishes[it+notNumber..fishes.size()-1]}"
// println("find right (${it}): ${fishes}")
findRight = true
}
}
def findLeft = false
(i-1..0).each {
if (findLeft) {
return
}
if (isNumber(fishes[it])) {
def ni = it - 1
def foundNotNumber = false
(it-1..0).each { ii ->
if (foundNotNumber) {
return
}
if (!isNumber(fishes[ii])) {
ni = ii
foundNotNumber = true
}
}
def number = fishes[ni+1..it] as Integer
fishes = "${fishes[0..ni]}${number+numbers[0]}${fishes[it+1..fishes.size()-1]}"
// println("find left (${it}): ${fishes}")
findLeft = true
}
}
}