Files
Advent-of-Code-2023/day5/dayFive.go
2023-12-05 09:50:19 +00:00

144 lines
3.6 KiB
Go

package day5
import (
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
type Almanach struct {
SeedToSoil,
SoilToFert,
FertToWater,
WaterToLight,
LightToTemp,
TempToHum,
HumToLocation EfficientMap
}
type Triplet struct {
target, source, rangeLen int
}
type EfficientMap struct {
mappings []Triplet
}
func (m EfficientMap) applyMap(source int) int {
for _, triplet := range m.mappings {
sourceStart, sourceEnd := triplet.source, triplet.source + triplet.rangeLen
if source >= sourceStart && source <= sourceEnd {
diff := source - sourceStart
return triplet.target + diff
}
}
return source
}
func applyMap(source int, mapping map[int]int) int {
var result int
result, found := mapping[source]
if !found {
result = source
}
return result
}
func (a Almanach)locationForSeed(seed int) int {
log.Print("starting calc of location for seed %d", seed)
soil := a.SeedToSoil.applyMap(seed)
fert := a.SoilToFert.applyMap(soil)
water := a.FertToWater.applyMap(fert)
light := a.WaterToLight.applyMap(water)
temp := a.LightToTemp.applyMap(light)
hum := a.TempToHum.applyMap(temp)
location := a.HumToLocation.applyMap(hum)
return location
}
func Run() int {
fmt.Println("the day 5")
inputDir := "day5/input"
seedToSoil := fmt.Sprint(inputDir, "/seed-to-soil")
log.Print("finished seed map")
soilToFert := fmt.Sprint(inputDir, "/soil-to-fertilizer")
log.Print("finished soil map")
fertToWater := fmt.Sprint(inputDir, "/fertilizer-to-water")
log.Print("finished fert map")
waterToLight := fmt.Sprint(inputDir, "/water-to-light")
log.Print("finished water map")
lightToTemp := fmt.Sprint(inputDir, "/light-to-temperature")
log.Print("finished light map")
tempToHum := fmt.Sprint(inputDir, "/temperature-to-humidity")
log.Print("finished temp map")
humToLocation := fmt.Sprint(inputDir, "/humidity-to-location")
seedsFileName := fmt.Sprint(inputDir, "/seeds")
seedsBytes, err := os.ReadFile(seedsFileName)
if err != nil {
panic(fmt.Sprint("error reading seeds file ", seedsFileName))
}
seedsLine := string(seedsBytes)
var seeds []int
for _, seedStr := range strings.Fields(seedsLine) {
seed, err := strconv.Atoi(seedStr)
if err != nil {
panic(fmt.Sprint("can't read seeds ", seedStr))
}
seeds = append(seeds, seed)
}
log.Print("finished for seeds file", seeds)
almanach := Almanach{
SeedToSoil: ReadMap(seedToSoil),
SoilToFert: ReadMap(soilToFert),
FertToWater: ReadMap(fertToWater),
WaterToLight: ReadMap(waterToLight),
LightToTemp: ReadMap(lightToTemp),
TempToHum: ReadMap(tempToHum),
HumToLocation: ReadMap(humToLocation),
}
log.Print("created almanach")
result := math.MaxInt
log.Print("before seed range")
for _, seed := range seeds {
log.Printf("checking for seed %d", seed)
location := almanach.locationForSeed(seed)
if location < result {
result = location
}
}
return result
}
func ReadMap(filename string) EfficientMap {
log.Printf("reading in map from %s", filename)
result := make([]Triplet, 0)
bytes, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprintf("error reading file %s", filename))
}
text := string(bytes)
text = strings.TrimSpace(text)
for _, line := range strings.Split(text, "\n") {
nums := strings.Fields(line)
if len(nums) != 3 {
panic(fmt.Sprint("error, map line doesn't have 3 nums: ", line, nums))
}
destinaitonStart, err1 := strconv.Atoi(nums[0])
sourceStart, err2 := strconv.Atoi(nums[1])
rangeLength, err3 := strconv.Atoi(nums[2])
if err1 != nil || err2 != nil || err3 != nil {
panic(fmt.Sprint("error converting one of the numbers: ", nums))
}
result = append(result, Triplet{destinaitonStart, sourceStart, rangeLength})
}
return EfficientMap{result}
}