-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.go
62 lines (54 loc) · 1.05 KB
/
part2.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
package day05
import (
"slices"
"strconv"
"strings"
"github.com/patrick22414/Advent-of-Code-2024/readinput"
)
func Part2() int {
rules := make(map[int][]int, 0)
input := readinput.ReadInput("./input.txt")
for line := range input {
a, b, ok := strings.Cut(line, "|")
if !ok {
break
}
x, err := strconv.Atoi(a)
if err != nil {
panic(err)
}
y, err := strconv.Atoi(b)
if err != nil {
panic(err)
}
rules[x] = append(rules[x], y)
}
// scan for books (sequences of pages)
books := make([][]int, 0)
for line := range input {
pages := strings.Split(line, ",")
book := make([]int, 0)
for _, p := range pages {
n, err := strconv.Atoi(p)
if err != nil {
panic(err)
}
book = append(book, n)
}
books = append(books, book)
}
total := 0
for _, book := range books {
book2 := slices.Clone(book)
slices.SortFunc(book2, func(a, b int) int {
if slices.Contains(rules[a], b) {
return -1 // a < b
}
return 0
})
if !slices.Equal(book, book2) {
total += book2[len(book2)/2]
}
}
return total
}