-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10p2.odin
97 lines (82 loc) · 1.87 KB
/
10p2.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
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
package main
import "core:fmt"
import "core:slice"
import "core:strings"
import "core:unicode/utf8"
D10P2 :: proc() {
input_string := #load("./inputs/10.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
middle_autocomplete_score := get_middle_autocomplete_score(lines)
fmt.printfln("Middle Autocomplete Score: %v", middle_autocomplete_score)
}
get_middle_autocomplete_score :: proc(input: []string) -> int {
completions: [dynamic]string
defer delete(completions)
for line in input {
completion := complete_navigation_line(line)
if completion != "" {
append(&completions, completion)
}
}
scores: [dynamic]int
defer delete(scores)
for completion in completions {
append(&scores, score_completion(completion))
}
slice.sort(scores[:])
return scores[len(scores) / 2]
}
complete_navigation_line :: proc(line: string) -> string {
opened: [dynamic]rune
closed: [dynamic]rune
matches := map[rune]rune {
')' = '(',
']' = '[',
'}' = '{',
'>' = '<',
}
inverse_matches := map[rune]rune {
'(' = ')',
'[' = ']',
'{' = '}',
'<' = '>',
}
defer {
delete(opened)
delete(closed)
delete(matches)
delete(inverse_matches)
}
for char in line {
switch char {
case '(', '[', '{', '<':
append(&opened, char)
case ')', ']', '}', '>':
if len(opened) == 0 || opened[len(opened) - 1] != matches[char] {
return ""
}
ordered_remove(&opened, len(opened) - 1)
}
}
// close the opened brackets in reverse order
for i := len(opened) - 1; i >= 0; i -= 1 {
append(&closed, inverse_matches[opened[i]])
}
// return as string
return utf8.runes_to_string(closed[:], context.temp_allocator)
}
score_completion :: proc(line: string) -> int {
values := map[rune]int {
')' = 1,
']' = 2,
'}' = 3,
'>' = 4,
}
defer delete(values)
score := 0
for char in line {
score *= 5
score += values[char]
}
return score
}