-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.go
53 lines (50 loc) · 1008 Bytes
/
10.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
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func index(c rune, a []rune) int {
for i, r := range a {
if c == r {
return i
}
}
return -1
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
score := 0
scores := make([]int, 0, 1000)
open := []rune{'(', '[', '{', '<'}
closing := []rune{')', ']', '}', '>'}
scoreMap := []int{3, 57, 1197, 25137}
for scanner.Scan() {
line := scanner.Text()
stack := make([]rune, 0, 1000)
i := 0
c := rune(' ')
for i, c = range line {
if index(c, open) >= 0 {
stack = append(stack, c)
} else {
cc := stack[len(stack)-1]
if index(c, closing) != index(cc, open) {
score += scoreMap[index(c, closing)]
break
}
stack = stack[:len(stack)-1]
}
}
if i == len(line)-1 {
completion := 0
for i := len(stack) - 1; i >= 0; i-- {
completion = completion*5 + index(stack[i], open) + 1
}
scores = append(scores, completion)
}
}
sort.Ints(scores)
fmt.Println(score, scores[len(scores)/2])
}