2023-12-05 19:06:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-12-05 20:01:55 +00:00
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/advent-of-code/internal/common"
|
2023-12-05 19:06:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
seedToSoilMap = "seed-to-soil map"
|
|
|
|
soilToFertilizerMap = "soil-to-fertilizer map"
|
|
|
|
fertilizerToWaterMap = "fertilizer-to-water map"
|
|
|
|
waterToLightMap = "water-to-light map"
|
|
|
|
lightToTemperatureMap = "light-to-temperature map"
|
|
|
|
temperatureToHumidityMap = "temperature-to-humidity map"
|
|
|
|
humidityToLocationMap = "humidity-to-location map"
|
|
|
|
)
|
|
|
|
|
|
|
|
type almanac struct {
|
|
|
|
seeds []int
|
|
|
|
seedToSoil map[int]int
|
|
|
|
soilToFertilizer map[int]int
|
|
|
|
fertilizerToWater map[int]int
|
|
|
|
waterToLight map[int]int
|
|
|
|
lightToTemperature map[int]int
|
|
|
|
temperatureToHumidity map[int]int
|
|
|
|
humidityToLocation map[int]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAlmanac(contents []string) (almanac, error) {
|
|
|
|
// Assumption: The first line always contains the seed numbers. An error is returned
|
|
|
|
// if this is not the case or if the format is all wrong.
|
|
|
|
seeds, err := parseSeedNums(contents[0])
|
|
|
|
if err != nil {
|
|
|
|
return almanac{}, fmt.Errorf("unable to parse the seed numbers; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapLines = []string{}
|
|
|
|
// loop while contents > 0
|
|
|
|
// if line is blank
|
|
|
|
// if map lines > 0 -> parse map and populate relevant field
|
|
|
|
// mapLines = []string{}
|
|
|
|
// else
|
|
|
|
// if first rune is digit, add line to mapLines:
|
|
|
|
// else get map name
|
|
|
|
|
|
|
|
output := almanac{
|
|
|
|
seeds: seeds,
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSeedNums(seedStr string) ([]int, error) {
|
|
|
|
expectedPrefix := "seeds:"
|
|
|
|
|
|
|
|
if !strings.HasPrefix(seedStr, expectedPrefix) {
|
|
|
|
return nil, fmt.Errorf("%q does not begin with %s", seedStr, expectedPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
seedStr = seedStr[strings.Index(seedStr, ":")+1:]
|
|
|
|
|
2023-12-05 20:01:55 +00:00
|
|
|
seeds, err := common.ParseIntList(seedStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse the seed numbers from %q; %w", seedStr, err)
|
2023-12-05 19:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return seeds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMap(mapLines []string) (map[int]int, error) {
|
2023-12-05 20:01:55 +00:00
|
|
|
//var mapMatrix [][]int
|
2023-12-05 19:06:59 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|