-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (67 loc) · 1.09 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"io"
"os"
"github.com/devries/advent_of_code_2021/utils"
)
func main() {
f, err := os.Open("../inputs/day10.txt")
utils.Check(err, "error opening input.txt")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int {
lines := utils.ReadLines(r)
total := 0
for _, l := range lines {
s := NewStack()
outer:
for _, r := range l {
switch r {
case '(', '[', '{', '<':
s.Push(r)
case ')', ']', '}', '>':
sr, ok := s.Pop()
if ok == false {
fmt.Println("Exhausted Stack")
break outer
}
if sr != matching[r] {
total += points[r]
break outer
}
}
}
}
return total
}
var points = map[rune]int{
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
var matching = map[rune]rune{
')': '(',
']': '[',
'}': '{',
'>': '<',
}
type Stack []rune
func NewStack() *Stack {
r := make(Stack, 0)
return &r
}
func (s *Stack) Push(r rune) {
*s = append(*s, r)
}
func (s *Stack) Pop() (rune, bool) {
if len(*s) == 0 {
return 0, false
}
r := (*s)[len(*s)-1]
*s = (*s)[0 : len(*s)-1]
return r, true
}