-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarden.go
288 lines (238 loc) · 6.68 KB
/
garden.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package day05
import (
"fmt"
"github.com/user3690/advent-of-code-in-go/util"
"log"
"strings"
"time"
"unicode"
)
type extendedSeed struct {
srcSeed int64
rangeSeed int64
}
type almaValues struct {
DestRangeStart int64
SrcRangeStart int64
Range int64
}
type almaMap struct {
AlmaValues []almaValues
}
type almanac struct {
Seeds []int64
extSeeds []extendedSeed
SeedToSoil almaMap
SoilToFertilizer almaMap
FertilizerToWater almaMap
WaterToLight almaMap
LightToTemperature almaMap
TemperatureToHumidity almaMap
HumidityToLocation almaMap
}
func (am *almaMap) getDestForSrc(src int64) (dest int64) {
dest = src
for _, values := range am.AlmaValues {
if src < values.SrcRangeStart || src > values.SrcRangeStart+values.Range-1 {
continue
}
if values.SrcRangeStart < values.DestRangeStart {
dest = src + (values.DestRangeStart - values.SrcRangeStart)
break
}
if values.SrcRangeStart > values.DestRangeStart {
dest = src - (values.SrcRangeStart - values.DestRangeStart)
break
}
}
return dest
}
// Part 1 -> 57075758
// Part 2 -> 31161857 2m38s
func BothParts() {
var (
lines []string
alma almanac
part1, part2 int64
err error
)
start := time.Now()
lines, err = util.ReadFileInLines("./2023/day05/input.txt")
if err != nil {
log.Fatal(err)
}
alma, err = prepareAlmanac(lines)
if err != nil {
log.Fatal(err)
}
part1, part2 = findLowestLocationId(alma)
elapsed := time.Since(start)
log.Printf("execution took %s", elapsed)
fmt.Println(part1, part2)
}
func prepareAlmanac(lines []string) (alma almanac, err error) {
var (
seeds []int64
section string
newAlmaValue almaValues
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemperature,
temperatureToHumidity,
humidityToLocation []almaValues
extSeeds []extendedSeed
)
for _, line := range lines {
if unicode.IsLetter(rune(line[0])) {
switch true {
case strings.Contains(line, "seeds"):
splitLine := strings.Split(line, ":")
seeds, err = util.FindNumbers(splitLine[1])
if err != nil {
return alma, err
}
extSeeds = buildSeedsForPart2(seeds)
case strings.Contains(line, "seed-to-soil"):
section = "seed-to-soil"
case strings.Contains(line, "soil-to-fertilizer"):
section = "soil-to-fertilizer"
case strings.Contains(line, "fertilizer-to-water"):
section = "fertilizer-to-water"
case strings.Contains(line, "water-to-light"):
section = "water-to-light"
case strings.Contains(line, "light-to-temperature"):
section = "light-to-temperature"
case strings.Contains(line, "temperature-to-humidity"):
section = "temperature-to-humidity"
case strings.Contains(line, "humidity-to-location"):
section = "humidity-to-location"
}
}
if unicode.IsNumber(rune(line[0])) {
switch section {
case "seed-to-soil":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
seedToSoil = append(seedToSoil, newAlmaValue)
case "soil-to-fertilizer":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
soilToFertilizer = append(soilToFertilizer, newAlmaValue)
case "fertilizer-to-water":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
fertilizerToWater = append(fertilizerToWater, newAlmaValue)
case "water-to-light":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
waterToLight = append(waterToLight, newAlmaValue)
case "light-to-temperature":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
lightToTemperature = append(lightToTemperature, newAlmaValue)
case "temperature-to-humidity":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
temperatureToHumidity = append(temperatureToHumidity, newAlmaValue)
case "humidity-to-location":
newAlmaValue, err = buildAlmaMaps(line)
if err != nil {
return alma, err
}
humidityToLocation = append(humidityToLocation, newAlmaValue)
}
}
}
alma.Seeds = seeds
alma.extSeeds = extSeeds
alma.SeedToSoil.AlmaValues = seedToSoil
alma.SoilToFertilizer.AlmaValues = soilToFertilizer
alma.FertilizerToWater.AlmaValues = fertilizerToWater
alma.WaterToLight.AlmaValues = waterToLight
alma.LightToTemperature.AlmaValues = lightToTemperature
alma.TemperatureToHumidity.AlmaValues = temperatureToHumidity
alma.HumidityToLocation.AlmaValues = humidityToLocation
return alma, err
}
func buildAlmaMaps(line string) (newAlmaValue almaValues, err error) {
var numbers []int64
numbers, err = util.FindNumbers(line)
if err != nil {
return newAlmaValue, err
}
newAlmaValue.DestRangeStart = numbers[0]
newAlmaValue.SrcRangeStart = numbers[1]
newAlmaValue.Range = numbers[2]
return newAlmaValue, err
}
func buildSeedsForPart2(seeds []int64) []extendedSeed {
var extSeeds = make([]extendedSeed, 10)
var j int
for i, seed := range seeds {
if i%2 == 0 {
start := seed
seedRange := seeds[i+1]
extSeeds[j] = extendedSeed{
srcSeed: start,
rangeSeed: seedRange,
}
j++
}
}
return extSeeds
}
func findLowestLocationId(alma almanac) (int64, int64) {
var (
lowestLocationPart1, lowestLocationPart2 int64
)
for _, seed := range alma.Seeds {
soil := alma.SeedToSoil.getDestForSrc(seed)
fertilizer := alma.SoilToFertilizer.getDestForSrc(soil)
water := alma.FertilizerToWater.getDestForSrc(fertilizer)
light := alma.WaterToLight.getDestForSrc(water)
temperature := alma.LightToTemperature.getDestForSrc(light)
humidity := alma.TemperatureToHumidity.getDestForSrc(temperature)
location := alma.HumidityToLocation.getDestForSrc(humidity)
if lowestLocationPart1 == 0 {
lowestLocationPart1 = location
}
if lowestLocationPart1 > location {
lowestLocationPart1 = location
}
}
for _, extSeed := range alma.extSeeds {
seed := extSeed.srcSeed
seedRange := extSeed.srcSeed + extSeed.rangeSeed
for seed < seedRange {
soil := alma.SeedToSoil.getDestForSrc(seed)
fertilizer := alma.SoilToFertilizer.getDestForSrc(soil)
water := alma.FertilizerToWater.getDestForSrc(fertilizer)
light := alma.WaterToLight.getDestForSrc(water)
temperature := alma.LightToTemperature.getDestForSrc(light)
humidity := alma.TemperatureToHumidity.getDestForSrc(temperature)
location := alma.HumidityToLocation.getDestForSrc(humidity)
if lowestLocationPart2 == 0 {
lowestLocationPart2 = location
}
if lowestLocationPart2 > location {
lowestLocationPart2 = location
}
seed++
}
}
return lowestLocationPart1, lowestLocationPart2
}