-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (60 loc) · 1012 Bytes
/
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
package main
import (
"fmt"
"slices"
"strings"
"github.com/devkvlt/aoc"
)
var (
rules map[string][]string
updates [][]string
)
func init() {
chunks := aoc.Chunks("input")
rules = map[string][]string{}
for _, line := range chunks[0] {
lr := strings.Split(line, "|")
rules[lr[0]] = append(rules[lr[0]], lr[1])
}
updates = make([][]string, len(chunks[1]))
for i, line := range chunks[1] {
updates[i] = strings.Split(line, ",")
}
}
func cmp(a, b string) int {
if slices.Contains(rules[a], b) {
return -1
}
return 1
}
func check(u []string) bool {
for i := 1; i < len(u); i++ {
if cmp(u[i-1], u[i]) > 0 {
return false
}
}
return true
}
func part1() {
sum := 0
for _, u := range updates {
if check(u) {
sum += aoc.Atoi(u[len(u)/2])
}
}
fmt.Println(sum)
}
func part2() {
sum := 0
for _, u := range updates {
if !check(u) {
slices.SortFunc(u, cmp)
sum += aoc.Atoi(u[len(u)/2])
}
}
fmt.Println(sum)
}
func main() {
part1() // 4609
part2() // 5723
}