-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.go
More file actions
193 lines (155 loc) · 4.32 KB
/
day05.go
File metadata and controls
193 lines (155 loc) · 4.32 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"AdventOfCode-go/advent2025/utils"
"fmt"
"strconv"
"strings"
)
type freshRange struct {
start int
end int
}
func smallest(first int, second int) int {
if first < second {
return first
}
return second
}
func largest(first int, second int) int {
if first > second {
return first
}
return second
}
func overlappingRange(first freshRange, second freshRange) bool {
if ((first.end >= second.start) && (first.end <= second.end)) ||
((first.start >= second.start) && (first.start <= second.end)) {
return true
}
return false
}
func day05(filename string, part byte, debug bool) int {
var result int
puzzleInput, _ := utils.ReadFile(filename)
// Get the size of the fresh ingredients array that we need
var countArraySize int
for _, puzzleLine := range puzzleInput {
if len(puzzleLine) == 0 {
break
}
countArraySize++
}
if debug {
fmt.Println("Array Size:", countArraySize)
}
ingredientDB := make([]freshRange, countArraySize)
var checkIngredients bool = false
for i, puzzleLine := range puzzleInput {
if !checkIngredients {
// Process fresh ingredient ID ranges until we see the blank line
if len(puzzleLine) == 0 {
if debug {
fmt.Println("found the blank line")
}
checkIngredients = true
} else {
ingredientRange := strings.Split(puzzleLine, "-")
ingredientDB[i].start, _ = strconv.Atoi(ingredientRange[0])
ingredientDB[i].end, _ = strconv.Atoi(ingredientRange[1])
}
} else {
if part == 'a' {
// Check each ingredient in the fresh ingredient DB we previously built
ingredient, _ := strconv.Atoi(puzzleLine)
for _, freshStuff := range ingredientDB {
if ingredient >= freshStuff.start && ingredient <= freshStuff.end {
result++
break
}
}
} else {
break
}
}
}
if part == 'a' {
return result
}
// part b. Don't process the ingredient list. Focus on the DB
// Need to work out how many unique fresh IDs there are
// Use the ranges in the DB
// look for overlapping ranges and COMBINE them
// once everything has been combined, add up the remaining IDs
expandedDB := make(map[freshRange]bool, 0)
for _, ingredientRange := range ingredientDB {
var didOverlap bool = false
for expandedRange := range expandedDB {
if overlappingRange(ingredientRange, expandedRange) {
// Combine the ranges
didOverlap = true
overlapRange := freshRange{smallest(ingredientRange.start, expandedRange.start), largest(ingredientRange.end, expandedRange.end)}
expandedDB[overlapRange] = true
if overlapRange != ingredientRange {
delete(expandedDB, ingredientRange)
}
if overlapRange != expandedRange {
delete(expandedDB, expandedRange)
}
}
}
// If no overlap then just add that ingredient range to our list
if !didOverlap {
expandedDB[ingredientRange] = true
}
}
// Now repeatedly loop through our expanded range looking for ranges that might overlap. Combine them, then look again
var madeAChange bool = true
for madeAChange {
madeAChange = false
for firstItem := range expandedDB {
for secondItem := range expandedDB {
if firstItem != secondItem {
if overlappingRange(firstItem, secondItem) {
// Combine the ranges
madeAChange = true
overlapRange := freshRange{smallest(firstItem.start, secondItem.start), largest(firstItem.end, secondItem.end)}
expandedDB[overlapRange] = true
if overlapRange != firstItem {
delete(expandedDB, firstItem)
}
if overlapRange != secondItem {
delete(expandedDB, secondItem)
}
}
}
}
}
}
// Sum up all the fresh ingredients in all the ranges
for i := range expandedDB {
result += i.end - i.start + 1
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", day05(filenamePtr, execPart, debug))
}
}
/*
List 1 - original list of stuff
List 2 - new, expanded list of stuff
For each entry in List 1
check through List 2
if nothing overlapping, append to List 2
if overlapping
change overlapping entry to include List 1 entry
check through all of List 2
doesn't anything else now overlap?
if overlap
combine entries and remove 1 of them
*/