-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10p1.odin
60 lines (49 loc) · 1.04 KB
/
10p1.odin
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
package main
import "core:fmt"
import "core:strings"
D10P1 :: proc() {
input_string := #load("./inputs/10.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
syntax_error_score := get_syntax_error_score(lines)
fmt.printfln("Syntax Error Score: %v", syntax_error_score)
}
get_syntax_error_score :: proc(input: []string) -> int {
score := 0
for line in input {
score += get_line_score(line)
}
return score
}
get_line_score :: proc(line: string) -> int {
score := 0
opened: [dynamic]rune
matches := map[rune]rune {
')' = '(',
']' = '[',
'}' = '{',
'>' = '<',
}
error_score := map[rune]int {
')' = 3,
']' = 57,
'}' = 1197,
'>' = 25137,
}
defer {
delete(opened)
delete(matches)
delete(error_score)
}
for char in line {
switch char {
case '(', '[', '{', '<':
append(&opened, char)
case ')', ']', '}', '>':
if len(opened) == 0 || opened[len(opened) - 1] != matches[char] {
return error_score[char]
}
ordered_remove(&opened, len(opened) - 1)
}
}
return score
}