-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (70 loc) · 2.01 KB
/
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
70
71
72
73
74
75
76
77
package main
import (
"aoc/internal/set"
"aoc/internal/utils"
_ "embed"
"fmt"
"slices"
"strings"
)
//go:embed input.txt
var input string
func handleRules(beforeSet Rulebook, afterSet Rulebook, updates [][]int) (int, int) {
part1Sum, part2Sum := 0, 0
for _, update := range updates {
isValid := true
seen := make(set.Set[int])
shouldNotSeeAgain := make(set.Set[int])
for currIdx, pageNumber := range update {
if shouldNotSeeAgain.Has(pageNumber) {
intersection := beforeSet[pageNumber].Intersection(seen)
lowestIdx := len(update)
for _, x := range intersection.Slice() {
xIdx := slices.Index(update, x)
lowestIdx = min(xIdx, lowestIdx)
}
update = slices.Delete(update, currIdx, currIdx+1)
update = slices.Insert(update, lowestIdx, pageNumber)
isValid = false
}
afterRules := afterSet[pageNumber]
shouldNotSeeAgain.Add(afterRules.Slice()...)
seen.Add(pageNumber)
}
middleIdx := len(update) / 2
if isValid {
part1Sum += update[middleIdx]
} else {
part2Sum += update[middleIdx]
}
}
return part1Sum, part2Sum
}
type Rulebook map[int]set.Set[int]
func (r Rulebook) AddOrInit(key int, item int) {
if _, ok := r[key]; !ok {
r[key] = make(set.Set[int])
}
r[key].Add(item)
}
func processInput(i string) (Rulebook, Rulebook, [][]int) {
sections := strings.Split(i, "\n\n")
beforeSet, afterSet := make(Rulebook), make(Rulebook)
rulesSection := strings.Fields(sections[0])
for _, rule := range rulesSection {
pages := strings.Split(rule, "|")
before, after := utils.StrToInt(pages[0]), utils.StrToInt(pages[1])
beforeSet.AddOrInit(before, after)
afterSet.AddOrInit(after, before)
}
updates := utils.Map(strings.Fields(sections[1]), func(row string) []int {
return utils.Map(strings.Split(row, ","), utils.StrToInt)
})
return beforeSet, afterSet, updates
}
func main() {
beforeSet, afterSet, instructions := processInput(input)
part1, part2 := handleRules(beforeSet, afterSet, instructions)
fmt.Println("Part 1: ", part1)
fmt.Println("Part 2: ", part2)
}