day5, example
This commit is contained in:
parent
e05226f6aa
commit
cebdd72171
|
@ -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
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
49 53 8
|
||||||
|
0 11 42
|
||||||
|
42 0 7
|
||||||
|
57 7 4
|
|
@ -0,0 +1,2 @@
|
||||||
|
60 56 37
|
||||||
|
56 93 4
|
|
@ -0,0 +1,3 @@
|
||||||
|
45 77 23
|
||||||
|
81 45 19
|
||||||
|
68 64 13
|
|
@ -0,0 +1,2 @@
|
||||||
|
50 98 2
|
||||||
|
52 50 48
|
|
@ -0,0 +1 @@
|
||||||
|
79 14 55 13
|
|
@ -0,0 +1,3 @@
|
||||||
|
0 15 37
|
||||||
|
37 52 2
|
||||||
|
39 0 15
|
|
@ -0,0 +1,2 @@
|
||||||
|
0 69 1
|
||||||
|
1 0 69
|
|
@ -0,0 +1,2 @@
|
||||||
|
88 18 7
|
||||||
|
18 25 70
|
4
main.go
4
main.go
|
@ -3,11 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sunshine.industries/aoc2023/day4"
|
"sunshine.industries/aoc2023/day5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Print("> starting run:")
|
log.Print("> starting run:")
|
||||||
result := day4.Run()
|
result := day5.Run()
|
||||||
log.Printf("day4 result: %d\n****\n", result)
|
log.Printf("day4 result: %d\n****\n", result)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue