-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.go
More file actions
153 lines (130 loc) · 4.58 KB
/
day05.go
File metadata and controls
153 lines (130 loc) · 4.58 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
package main
import (
"AdventOfCode-go/advent2023/utils"
"fmt"
"strconv"
"strings"
)
type rangesStruct struct {
destinationStart int
sourceStart int
rangeLength int
}
func findDestination(mapping []rangesStruct, item int) int {
var difference int
for i := 0; i < len(mapping); i++ {
if item >= mapping[i].sourceStart && item <= mapping[i].sourceStart+mapping[i].rangeLength {
difference = item - mapping[i].sourceStart
return mapping[i].destinationStart + difference
}
}
// If it doesn't match a range in the map, it maps to the same value
return item
}
func buildMap(line int, puzzleInput []string) []rangesStruct {
var lineValues rangesStruct
var returnMapping []rangesStruct
for i := line + 1; i < len(puzzleInput) && puzzleInput[i] != ""; i++ {
fmt.Sscanf(puzzleInput[i], "%d %d %d", &lineValues.destinationStart, &lineValues.sourceStart, &lineValues.rangeLength)
returnMapping = append(returnMapping, lineValues)
}
return returnMapping
}
//
//
//
func day05(filename string, part byte, debug bool) int {
puzzleInput, _ := utils.ReadFile(filename)
seeds := strings.Split(puzzleInput[0], ":")
seedNumbersStr := strings.Fields(strings.TrimSpace(seeds[1]))
//var seedNumbers []int
seedNumbers := make([]int, len(seedNumbersStr))
for key, value := range seedNumbersStr {
seedNumbers[key], _ = strconv.Atoi(value)
}
if debug {
for i, j := range seedNumbers {
fmt.Printf("Seed: %d Number: %d\n", i, j)
}
fmt.Println(seedNumbersStr)
}
var seedToSoil, soilToFertilizer, fertilizerToWater, waterToLight, lightToTemperature, temperatureToHumidity, humidityToLocation []rangesStruct
for line, puzzleLine := range puzzleInput {
if line == 0 {
// Skip the seed line
continue
}
switch puzzleLine {
case "seed-to-soil map:":
seedToSoil = buildMap(line, puzzleInput)
case "soil-to-fertilizer map:":
soilToFertilizer = buildMap(line, puzzleInput)
case "fertilizer-to-water map:":
fertilizerToWater = buildMap(line, puzzleInput)
case "water-to-light map:":
waterToLight = buildMap(line, puzzleInput)
case "light-to-temperature map:":
lightToTemperature = buildMap(line, puzzleInput)
case "temperature-to-humidity map:":
temperatureToHumidity = buildMap(line, puzzleInput)
case "humidity-to-location map:":
humidityToLocation = buildMap(line, puzzleInput)
}
}
var result int = 999999999
// Now we've built the maps, lets work through each seed
if part == 'a' {
var destinationFromSource int
for _, seed := range seedNumbers {
if debug {
fmt.Println("Seed:", seed)
}
destinationFromSource = findDestination(seedToSoil, seed)
destinationFromSource = findDestination(soilToFertilizer, destinationFromSource)
destinationFromSource = findDestination(fertilizerToWater, destinationFromSource)
destinationFromSource = findDestination(waterToLight, destinationFromSource)
destinationFromSource = findDestination(lightToTemperature, destinationFromSource)
destinationFromSource = findDestination(temperatureToHumidity, destinationFromSource)
destinationFromSource = findDestination(humidityToLocation, destinationFromSource)
if debug {
fmt.Println("Location:", destinationFromSource)
}
fmt.Printf("seed: %d location: %d\n", seed, destinationFromSource)
if destinationFromSource < result {
result = destinationFromSource
}
}
return result
}
var destinationFromSource int
for i := 0; i < len(seedNumbers); i += 2 {
for seed := seedNumbers[i]; seed < (seedNumbers[i] + seedNumbers[i+1]); seed++ {
destinationFromSource = findDestination(seedToSoil, seed)
destinationFromSource = findDestination(soilToFertilizer, destinationFromSource)
destinationFromSource = findDestination(fertilizerToWater, destinationFromSource)
destinationFromSource = findDestination(waterToLight, destinationFromSource)
destinationFromSource = findDestination(lightToTemperature, destinationFromSource)
destinationFromSource = findDestination(temperatureToHumidity, destinationFromSource)
destinationFromSource = findDestination(humidityToLocation, destinationFromSource)
if debug {
fmt.Printf("seed: %d location: %d\n", seed, destinationFromSource)
}
if destinationFromSource < result {
result = destinationFromSource
}
}
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
switch execPart {
case 'a':
fmt.Printf("Result is: %d\n", day05(filenamePtr, execPart, debug))
case 'b':
fmt.Printf("Result is: %d\n", day05(filenamePtr, execPart, debug))
default:
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}