-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.go
171 lines (143 loc) · 4.07 KB
/
day5.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bufio"
"maps"
"os"
"slices"
"strconv"
"strings"
"time"
)
func day5() {
println("Solving AoC 2024 Day 5:")
rules, updates := loadInputDay5()
invalidUpdates := day5part1(rules, updates)
day5part2(rules, invalidUpdates)
}
func loadInputDay5() ([][]int, [][]int) {
defer timeTrack(time.Now(), "Loading/Parsing Input")
file, err := os.Open("inputs/day5.txt")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
rules := make([][]int, 0)
updates := make([][]int, 0)
for scanner.Scan() {
var line = scanner.Text()
if strings.Contains(line, "|") {
// Parsing Rule
split := strings.Split(line, `|`)
constrained, _ := strconv.Atoi(split[0])
before, _ := strconv.Atoi(split[1])
rules = append(rules, []int{constrained, before})
} else if strings.Contains(line, ",") {
// Parsing update
split := strings.Split(line, `,`)
pages := make([]int, 0)
for _, s := range split {
page, _ := strconv.Atoi(s)
pages = append(pages, page)
}
updates = append(updates, pages)
}
}
return rules, updates
}
func day5part1(rules [][]int, updates [][]int) [][]int {
defer timeTrack(time.Now(), "Part1")
var middlePageSum = 0
invalidUpdates := make([][]int, 0)
// build PageRuleMap for ease of use
rulesMap := createRulesMap(rules)
for _, update := range updates {
// Build ignored rules for update
ignoredPages := getIgnoredPages(update, rulesMap)
// Check order of update
// For each page, check if conform to rules
valid := checkValidity(update, rulesMap, ignoredPages)
if valid {
middlePageSum += update[len(update)/2]
} else {
invalidUpdates = append(invalidUpdates, update)
}
}
println(middlePageSum)
return invalidUpdates
}
func createRulesMap(rules [][]int) map[int][]int {
rulesMap := make(map[int][]int)
for _, rule := range rules {
constrained := rule[0]
before := rule[1]
rulesMap[constrained] = append(rulesMap[constrained], before)
}
return rulesMap
}
func getIgnoredPages(update []int, rulesMap map[int][]int) []int {
ignoredPages := make([]int, 0)
for key := range maps.Keys(rulesMap) {
if !slices.Contains(update, key) {
ignoredPages = append(ignoredPages, key)
}
}
for valueSlice := range maps.Values(rulesMap) {
for _, value := range valueSlice {
if !slices.Contains(update, value) {
if !slices.Contains(ignoredPages, value) {
ignoredPages = append(ignoredPages, value)
}
}
}
}
return ignoredPages
}
func checkValidity(update []int, rulesMap map[int][]int, ignoredPages []int) bool {
valid := true
for i, page := range update {
constraints := rulesMap[page]
after := update[i:]
for _, constraint := range constraints {
if !slices.Contains(ignoredPages, constraint) && !slices.Contains(after, constraint) {
valid = false
}
}
}
return valid
}
func day5part2(rules [][]int, invalidUpdates [][]int) {
defer timeTrack(time.Now(), "Part2")
var middlePageSum = 0
rulesMap := createRulesMap(rules)
stillInvalid, middlePageSum := fixUpdates(invalidUpdates, rulesMap, middlePageSum)
for len(stillInvalid) > 0 {
stillInvalid, middlePageSum = fixUpdates(stillInvalid, rulesMap, middlePageSum)
}
println(middlePageSum)
}
func fixUpdates(invalidUpdates [][]int, rulesMap map[int][]int, middlePageSum int) ([][]int, int) {
stillInvalid := make([][]int, 0)
for _, update := range invalidUpdates {
ignoredPages := getIgnoredPages(update, rulesMap)
for i, page := range update {
constraints := rulesMap[page]
after := update[i+1:]
for _, constraint := range constraints {
if !slices.Contains(ignoredPages, constraint) && !slices.Contains(after, constraint) {
// swap places with colliding page
collidingPageIdx := slices.Index(update, constraint)
update[i], update[collidingPageIdx] = update[collidingPageIdx], update[i]
}
}
}
// Check order of update
// For each page, check if conform to rules
valid := checkValidity(update, rulesMap, ignoredPages)
if valid {
middlePageSum += update[len(update)/2]
} else {
stillInvalid = append(stillInvalid, update)
}
}
return stillInvalid, middlePageSum
}