diff --git a/day5/dayFive.go b/day5/dayFive.go new file mode 100644 index 0000000..1338688 --- /dev/null +++ b/day5/dayFive.go @@ -0,0 +1,113 @@ +package day5 + +import ( + "fmt" + "math" + "os" + "strconv" + "strings" +) + +type Almanach struct { + SeedToSoil, + SoilToFert, + FertToWater, + WaterToLight, + LightToTemp, + TempToHum, + HumToLocation map[int]int +} + +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 { + 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) + return location +} + +func Run() int { + fmt.Println("the day 5") + inputDir := "day5/example" + seedToSoil := fmt.Sprint(inputDir, "/seed-to-soil") + soilToFert := fmt.Sprint(inputDir, "/soil-to-fertilizer") + fertToWater := fmt.Sprint(inputDir, "/fertilizer-to-water") + waterToLight := fmt.Sprint(inputDir, "/water-to-light") + lightToTemp := fmt.Sprint(inputDir, "/light-to-temperature") + tempToHum := fmt.Sprint(inputDir, "/temperature-to-humidity") + 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) + } + + almanach := Almanach{ + SeedToSoil: ReadMap(seedToSoil), + SoilToFert: ReadMap(soilToFert), + FertToWater: ReadMap(fertToWater), + WaterToLight: ReadMap(waterToLight), + LightToTemp: ReadMap(lightToTemp), + TempToHum: ReadMap(tempToHum), + HumToLocation: ReadMap(humToLocation), + } + + result := math.MaxInt + for _, seed := range seeds { + location := almanach.locationForSeed(seed) + if location < result { + result = location + } + } + + return result +} + +func ReadMap(filename string) map[int]int { + result := make(map[int]int) + 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)) + } + for i := 0; i < rangeLength; i++ { + result[sourceStart+i] = destinaitonStart + i + } + } + + return result +} diff --git a/day5/example/fertilizer-to-water b/day5/example/fertilizer-to-water new file mode 100644 index 0000000..ccdd333 --- /dev/null +++ b/day5/example/fertilizer-to-water @@ -0,0 +1,4 @@ +49 53 8 +0 11 42 +42 0 7 +57 7 4 diff --git a/day5/example/humidity-to-location b/day5/example/humidity-to-location new file mode 100644 index 0000000..5b04dc4 --- /dev/null +++ b/day5/example/humidity-to-location @@ -0,0 +1,2 @@ +60 56 37 +56 93 4 diff --git a/day5/example/light-to-temperature b/day5/example/light-to-temperature new file mode 100644 index 0000000..f9ff54a --- /dev/null +++ b/day5/example/light-to-temperature @@ -0,0 +1,3 @@ +45 77 23 +81 45 19 +68 64 13 diff --git a/day5/example/seed-to-soil b/day5/example/seed-to-soil new file mode 100644 index 0000000..f3c26a9 --- /dev/null +++ b/day5/example/seed-to-soil @@ -0,0 +1,2 @@ +50 98 2 +52 50 48 diff --git a/day5/example/seeds b/day5/example/seeds new file mode 100644 index 0000000..54b366c --- /dev/null +++ b/day5/example/seeds @@ -0,0 +1 @@ +79 14 55 13 diff --git a/day5/example/soil-to-fertilizer b/day5/example/soil-to-fertilizer new file mode 100644 index 0000000..4ce60bd --- /dev/null +++ b/day5/example/soil-to-fertilizer @@ -0,0 +1,3 @@ +0 15 37 +37 52 2 +39 0 15 diff --git a/day5/example/temperature-to-humidity b/day5/example/temperature-to-humidity new file mode 100644 index 0000000..8c7d9aa --- /dev/null +++ b/day5/example/temperature-to-humidity @@ -0,0 +1,2 @@ +0 69 1 +1 0 69 diff --git a/day5/example/water-to-light b/day5/example/water-to-light new file mode 100644 index 0000000..ccf6d0f --- /dev/null +++ b/day5/example/water-to-light @@ -0,0 +1,2 @@ +88 18 7 +18 25 70 diff --git a/main.go b/main.go index 255b2f2..27b7a92 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,11 @@ package main import ( "log" - "sunshine.industries/aoc2023/day4" + "sunshine.industries/aoc2023/day5" ) func main() { log.Print("> starting run:") - result := day4.Run() + result := day5.Run() log.Printf("day4 result: %d\n****\n", result) }