-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.go
113 lines (108 loc) · 2.26 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
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
package main
import (
"fmt"
"github.com/dergeberl/aoc/utils"
"os"
"sort"
)
var scores = map[rune]int{
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay10Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay10Part2(string(input)))
}
//SolveDay10Part1 returns the error score of all lines
func SolveDay10Part1(input string) int {
line, _ := utils.InputToSlice(input)
var sum int
for i := range line {
nextClosing := []rune{}
errorPoints := 0
for c := range line[i] {
switch line[i][c] {
case '(':
nextClosing = append(nextClosing, ')')
continue
case '{':
nextClosing = append(nextClosing, '}')
continue
case '[':
nextClosing = append(nextClosing, ']')
continue
case '<':
nextClosing = append(nextClosing, '>')
continue
default:
if nextClosing[len(nextClosing)-1] != rune(line[i][c]) {
errorPoints = scores[rune(line[i][c])]
}
nextClosing = nextClosing[:len(nextClosing)-1]
}
if errorPoints != 0 {
sum += errorPoints
break
}
}
}
return sum
}
//SolveDay10Part2 returns middle the completion score
func SolveDay10Part2(input string) int {
line, _ := utils.InputToSlice(input)
var sum []int
for i := range line {
nextClosing := []rune{}
syntaxError := false
completionScore := 0
for c := range line[i] {
switch line[i][c] {
case '(':
nextClosing = append(nextClosing, ')')
continue
case '{':
nextClosing = append(nextClosing, '}')
continue
case '[':
nextClosing = append(nextClosing, ']')
continue
case '<':
nextClosing = append(nextClosing, '>')
continue
default:
if nextClosing[len(nextClosing)-1] != rune(line[i][c]) {
syntaxError = true
}
nextClosing = nextClosing[:len(nextClosing)-1]
}
if syntaxError {
break
}
}
if !syntaxError {
for n := range nextClosing {
completionScore *= 5
switch nextClosing[len(nextClosing)-1-n] {
case ')':
completionScore += 1
case ']':
completionScore += 2
case '}':
completionScore += 3
case '>':
completionScore += 4
}
}
sum = append(sum, completionScore)
}
}
sort.Ints(sum)
return sum[len(sum)/2]
}