day5, part2. making map efficient
This commit is contained in:
@@ -2,6 +2,7 @@ package day5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -15,7 +16,26 @@ type Almanach struct {
|
||||
WaterToLight,
|
||||
LightToTemp,
|
||||
TempToHum,
|
||||
HumToLocation map[int]int
|
||||
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 {
|
||||
@@ -28,25 +48,32 @@ func applyMap(source int, mapping map[int]int) int {
|
||||
}
|
||||
|
||||
func (a Almanach)locationForSeed(seed int) int {
|
||||
soil := applyMap(seed, a.SeedToSoil)
|
||||
fert := applyMap(soil, a.SoilToFert)
|
||||
water := applyMap(fert, a.FertToWater)
|
||||
light := applyMap(water, a.WaterToLight)
|
||||
temp := applyMap(light, a.LightToTemp)
|
||||
hum := applyMap(temp, a.TempToHum)
|
||||
location := applyMap(hum, a.HumToLocation)
|
||||
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/example"
|
||||
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")
|
||||
@@ -63,6 +90,7 @@ func Run() int {
|
||||
}
|
||||
seeds = append(seeds, seed)
|
||||
}
|
||||
log.Print("finished for seeds file", seeds)
|
||||
|
||||
almanach := Almanach{
|
||||
SeedToSoil: ReadMap(seedToSoil),
|
||||
@@ -73,9 +101,12 @@ func Run() int {
|
||||
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
|
||||
@@ -85,8 +116,9 @@ func Run() int {
|
||||
return result
|
||||
}
|
||||
|
||||
func ReadMap(filename string) map[int]int {
|
||||
result := make(map[int]int)
|
||||
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))
|
||||
@@ -104,10 +136,8 @@ func ReadMap(filename string) map[int]int {
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
panic(fmt.Sprint("error converting one of the numbers: ", nums))
|
||||
}
|
||||
for i := 0; i < rangeLength; i++ {
|
||||
result[sourceStart+i] = destinaitonStart + i
|
||||
}
|
||||
result = append(result, Triplet{destinaitonStart, sourceStart, rangeLength})
|
||||
}
|
||||
|
||||
return result
|
||||
return EfficientMap{result}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user