-
Notifications
You must be signed in to change notification settings - Fork 10
/
day05.go
64 lines (58 loc) · 1.17 KB
/
day05.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"slices"
"strconv"
"strings"
)
var inputFile = flag.String("inputFile", "inputs/day05.input", "Relative file path to use as input.")
func main() {
flag.Parse()
bytes, err := ioutil.ReadFile(*inputFile)
if err != nil {
return
}
contents := string(bytes)
split := strings.Split(contents, "\n")
rules := make(map[int][]int)
var clean int
var fixed int
parsingRules := true
for _, s := range split[:len(split)-1] {
if len(s) == 0 {
parsingRules = false
continue
}
if parsingRules {
parts := strings.Split(s, "|")
before, _ := strconv.Atoi(parts[0])
after, _ := strconv.Atoi(parts[1])
rules[before] = append(rules[before], after)
} else {
parts := strings.Split(s, ",")
var list []int
for _, v := range parts {
n, _ := strconv.Atoi(v)
list = append(list, n)
}
cmp := func(a, b int) int {
for _, v := range rules[b] {
if v == a {
return 1
}
}
return -1
}
if slices.IsSortedFunc(list, cmp) {
clean += list[len(list)/2]
} else {
slices.SortFunc(list, cmp)
fixed += list[len(list)/2]
}
}
}
fmt.Println(clean)
fmt.Println(fixed)
}