-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.go
72 lines (65 loc) · 1.64 KB
/
day10.go
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
package main
import "sort"
func calcErrorAndCompletionScore(lines []string) (int, int) {
syntaxPoint := map[byte]int{
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
completionPoint := map[byte]int{
')': 1,
']': 2,
'}': 3,
'>': 4,
}
closerMap := map[byte]byte{
'(': ')',
'[': ']',
'{': '}',
'<': '>',
}
errorSum, incompleteLines := extractErrorSumAndIncompleteLines(lines, closerMap, syntaxPoint)
completionSum := calcCompletionSum(incompleteLines, completionPoint, closerMap)
sort.Ints(completionSum)
return errorSum, completionSum[len(completionSum)/2]
}
func calcCompletionSum(incompleteLines []string, completionPoint map[byte]int, closerMap map[byte]byte) []int {
var completionSum []int
for _, v := range incompleteLines {
v = ReverseString(v)
var sum int = 0
for i := 0; i < len(v); i++ {
sum *= 5
sum += completionPoint[closerMap[v[i]]]
}
completionSum = append(completionSum, sum)
}
return completionSum
}
func extractErrorSumAndIncompleteLines(lines []string, closerMap map[byte]byte, syntaxPoint map[byte]int) (int, []string) {
var errorSum int
var incompleteLines []string
for _, v := range lines {
var closings []byte
var keepLine bool = true
for i := 0; i < len(v); i++ {
if v[i] == '(' ||
v[i] == '[' ||
v[i] == '{' ||
v[i] == '<' {
closings = append(closings, v[i])
} else if v[i] == closerMap[closings[len(closings)-1]] {
closings = RemoveLastElement(closings)
} else {
errorSum += syntaxPoint[v[i]]
keepLine = false
break
}
}
if keepLine {
incompleteLines = append(incompleteLines, string(closings))
}
}
return errorSum, incompleteLines
}