-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.go
75 lines (62 loc) · 1.35 KB
/
03.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
package main
import (
"fmt"
"strings"
)
func AOC202203Priority(input rune) int {
// a->z => 1->26
// A->Z => 27->52
if 'a' <= input && input <= 'z' {
return int(input - 'a' + 1)
}
return int(input - 'A' + 26 + 1)
}
func AOC202203Item(line string) rune {
partlen := len(line) / 2
// O(n^2), yeah!
for _, lRune := range line[:partlen] {
for _, rRune := range line[partlen:] {
if lRune == rRune {
return lRune
}
}
}
return ' '
}
func AOC202203Multiitem(first, second, third string) rune {
// O(n^3), yeah!
for _, lRune := range first {
for _, mRune := range second {
for _, rRune := range third {
if lRune == mRune && mRune == rRune {
return lRune
}
}
}
}
return ' '
}
func AOC202203Round(line string) int {
item := AOC202203Item(line)
prio := AOC202203Priority(item)
return prio
}
func AOC2022031(input string) (string, error) {
sum := 0
for _, line := range strings.Split(input, "\n") {
prio := AOC202203Round(line)
sum += prio
}
return fmt.Sprintf("%d", sum), nil
}
func AOC2022032(input string) (string, error) {
sum := 0
lines := strings.Split(input, "\n")
rounds := len(lines) / 3
for round := 0; round < rounds; round++ {
item := AOC202203Multiitem(lines[round*3], lines[round*3+1], lines[round*3+2])
prio := AOC202203Priority(item)
sum += prio
}
return fmt.Sprintf("%d", sum), nil
}