2023/05
This commit is contained in:
parent
ce22253dd8
commit
e7796247ee
11
2023/05/go.mod
Normal file
11
2023/05/go.mod
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module zv0n/advent_of_code/2023/05
|
||||||
|
|
||||||
|
go 1.21.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/fatih/color v1.16.0 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/thoas/go-funk v0.9.3 // indirect
|
||||||
|
golang.org/x/sys v0.14.0 // indirect
|
||||||
|
)
|
19
2023/05/go.sum
Normal file
19
2023/05/go.sum
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||||
|
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
|
||||||
|
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||||
|
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
218
2023/05/main.go
Normal file
218
2023/05/main.go
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readFileLines(filename string) ([]string, error) {
|
||||||
|
var lines []string
|
||||||
|
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return lines, err
|
||||||
|
}
|
||||||
|
fileScanner := bufio.NewScanner(file)
|
||||||
|
fileScanner.Split(bufio.ScanLines)
|
||||||
|
|
||||||
|
for fileScanner.Scan() {
|
||||||
|
lines = append(lines, fileScanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseInput(lines []string) (Input, []uint64, error) {
|
||||||
|
var input Input
|
||||||
|
var seeds []uint64
|
||||||
|
var seedMap, soilMap, fertilizerMap, waterMap, lightMap, temperatureMap, humidityMap bool
|
||||||
|
seedMap = false
|
||||||
|
soilMap = false
|
||||||
|
fertilizerMap = false
|
||||||
|
waterMap = false
|
||||||
|
lightMap = false
|
||||||
|
temperatureMap = false
|
||||||
|
humidityMap = false
|
||||||
|
for idx, line := range lines {
|
||||||
|
if idx == 0 {
|
||||||
|
numbers := strings.Split(strings.TrimSpace(strings.Split(line, ":")[1]), " ")
|
||||||
|
for _, num := range numbers {
|
||||||
|
seed, err := strconv.ParseUint(num, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return input, seeds, err
|
||||||
|
}
|
||||||
|
seeds = append(seeds, seed)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if len(line) == 0 {
|
||||||
|
seedMap = false
|
||||||
|
soilMap = false
|
||||||
|
fertilizerMap = false
|
||||||
|
waterMap = false
|
||||||
|
lightMap = false
|
||||||
|
temperatureMap = false
|
||||||
|
humidityMap = false
|
||||||
|
} else if unicode.IsDigit(rune(line[0])) {
|
||||||
|
numbers := strings.Split(line, " ")
|
||||||
|
var mapper Mapper
|
||||||
|
source, err := strconv.ParseUint(numbers[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return input, seeds, err
|
||||||
|
}
|
||||||
|
target, err := strconv.ParseUint(numbers[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return input, seeds, err
|
||||||
|
}
|
||||||
|
count, err := strconv.ParseUint(numbers[2], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return input, seeds, err
|
||||||
|
}
|
||||||
|
mapper.source = source
|
||||||
|
mapper.target = target
|
||||||
|
mapper.count = count
|
||||||
|
if seedMap {
|
||||||
|
input.seedToSoil = append(input.seedToSoil, mapper)
|
||||||
|
} else if soilMap {
|
||||||
|
input.soilToFertilizer = append(input.soilToFertilizer, mapper)
|
||||||
|
} else if fertilizerMap {
|
||||||
|
input.fertilizerToWater = append(input.fertilizerToWater, mapper)
|
||||||
|
} else if waterMap {
|
||||||
|
input.waterToLight = append(input.waterToLight, mapper)
|
||||||
|
} else if lightMap {
|
||||||
|
input.lightToTemperature = append(input.lightToTemperature, mapper)
|
||||||
|
} else if temperatureMap {
|
||||||
|
input.temperatureToHumidity = append(input.temperatureToHumidity, mapper)
|
||||||
|
} else if humidityMap {
|
||||||
|
input.humidityToLocation = append(input.humidityToLocation, mapper)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if line[:4] == "seed" {
|
||||||
|
seedMap = true
|
||||||
|
} else if line[:4] == "soil" {
|
||||||
|
soilMap = true
|
||||||
|
} else if line[:4] == "fert" {
|
||||||
|
fertilizerMap = true
|
||||||
|
} else if line[:4] == "wate" {
|
||||||
|
waterMap = true
|
||||||
|
} else if line[:4] == "ligh" {
|
||||||
|
lightMap = true
|
||||||
|
} else if line[:4] == "temp" {
|
||||||
|
temperatureMap = true
|
||||||
|
} else if line[:4] == "humi" {
|
||||||
|
humidityMap = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(MapperSort(input.seedToSoil))
|
||||||
|
sort.Sort(MapperSort(input.soilToFertilizer))
|
||||||
|
sort.Sort(MapperSort(input.fertilizerToWater))
|
||||||
|
sort.Sort(MapperSort(input.waterToLight))
|
||||||
|
sort.Sort(MapperSort(input.lightToTemperature))
|
||||||
|
sort.Sort(MapperSort(input.temperatureToHumidity))
|
||||||
|
sort.Sort(MapperSort(input.humidityToLocation))
|
||||||
|
return input, seeds, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateIntervals(intervals [][2]uint64, mapper []Mapper) [][2]uint64 {
|
||||||
|
var newIntervals [][2]uint64
|
||||||
|
for _, interval := range intervals {
|
||||||
|
finishedInterval := false
|
||||||
|
for _, mapper := range mapper {
|
||||||
|
if finishedInterval {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if interval[0] >= mapper.source && interval[0] < mapper.source+mapper.count {
|
||||||
|
parseInterval := interval
|
||||||
|
if interval[1] >= mapper.source+mapper.count {
|
||||||
|
parseInterval[1] = mapper.source + mapper.count - 1
|
||||||
|
interval[0] = mapper.source + mapper.count
|
||||||
|
} else {
|
||||||
|
finishedInterval = true
|
||||||
|
}
|
||||||
|
newIntervals = append(newIntervals, [2]uint64{parseInterval[0] - mapper.source + mapper.target, parseInterval[1] - mapper.source + mapper.target})
|
||||||
|
} else if interval[0] < mapper.source {
|
||||||
|
if interval[1] >= mapper.source {
|
||||||
|
newIntervals = append(newIntervals, [2]uint64{interval[0], mapper.source - 1})
|
||||||
|
interval = [2]uint64{mapper.source, interval[1]}
|
||||||
|
} else {
|
||||||
|
newIntervals = append(newIntervals, interval)
|
||||||
|
finishedInterval = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !finishedInterval {
|
||||||
|
newIntervals = append(newIntervals, interval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(IntervalSort(newIntervals))
|
||||||
|
var returnIntervals [][2]uint64
|
||||||
|
for _, interval := range newIntervals {
|
||||||
|
if len(returnIntervals) == 0 {
|
||||||
|
returnIntervals = append(returnIntervals, interval)
|
||||||
|
}
|
||||||
|
if returnIntervals[len(returnIntervals)-1][1] == interval[0] {
|
||||||
|
returnIntervals[len(returnIntervals)-1][1] = interval[1]
|
||||||
|
} else {
|
||||||
|
returnIntervals = append(returnIntervals, interval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnIntervals
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAlmanacTest(input Input) [][2]uint64 {
|
||||||
|
intervals := input.seedIntervals
|
||||||
|
intervals = calculateIntervals(intervals, input.seedToSoil)
|
||||||
|
intervals = calculateIntervals(intervals, input.soilToFertilizer)
|
||||||
|
intervals = calculateIntervals(intervals, input.fertilizerToWater)
|
||||||
|
intervals = calculateIntervals(intervals, input.waterToLight)
|
||||||
|
intervals = calculateIntervals(intervals, input.lightToTemperature)
|
||||||
|
intervals = calculateIntervals(intervals, input.temperatureToHumidity)
|
||||||
|
intervals = calculateIntervals(intervals, input.humidityToLocation)
|
||||||
|
return intervals
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
redPrint := color.New(color.FgRed)
|
||||||
|
yellowPrint := color.New(color.FgYellow)
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
redPrint.Fprintln(os.Stderr, "You must provide the input file")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
lines, err := readFileLines(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
redPrint.Fprintln(os.Stderr, "Could not read input file")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
input, seeds, err := parseInput(lines)
|
||||||
|
if err != nil {
|
||||||
|
redPrint.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, seed := range seeds {
|
||||||
|
input.seedIntervals = append(input.seedIntervals, [2]uint64{seed, seed})
|
||||||
|
}
|
||||||
|
sort.Sort(IntervalSort(input.seedIntervals))
|
||||||
|
locationsNew := parseAlmanacTest(input)
|
||||||
|
fmt.Print("Part 1: ")
|
||||||
|
yellowPrint.Print(locationsNew[0][0])
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
input.seedIntervals = [][2]uint64{}
|
||||||
|
for i := 0; i < len(seeds); i += 2 {
|
||||||
|
input.seedIntervals = append(input.seedIntervals, [2]uint64{seeds[i], seeds[i] + seeds[i+1] - 1})
|
||||||
|
}
|
||||||
|
sort.Sort(IntervalSort(input.seedIntervals))
|
||||||
|
locationsNew2 := parseAlmanacTest(input)
|
||||||
|
fmt.Print("Part 2: ")
|
||||||
|
yellowPrint.Print(locationsNew2[0][0])
|
||||||
|
fmt.Println()
|
||||||
|
}
|
30
2023/05/seed.go
Normal file
30
2023/05/seed.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type IntervalSort [][2]uint64
|
||||||
|
|
||||||
|
func (is IntervalSort) Len() int { return len(is) }
|
||||||
|
func (is IntervalSort) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
|
||||||
|
func (is IntervalSort) Less(i, j int) bool { return is[i][0] < is[j][0] }
|
||||||
|
|
||||||
|
type MapperSort []Mapper
|
||||||
|
|
||||||
|
func (ms MapperSort) Len() int { return len(ms) }
|
||||||
|
func (ms MapperSort) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] }
|
||||||
|
func (ms MapperSort) Less(i, j int) bool { return ms[i].source < ms[j].source }
|
||||||
|
|
||||||
|
type Input struct {
|
||||||
|
seedIntervals [][2]uint64
|
||||||
|
seedToSoil []Mapper
|
||||||
|
soilToFertilizer []Mapper
|
||||||
|
fertilizerToWater []Mapper
|
||||||
|
waterToLight []Mapper
|
||||||
|
lightToTemperature []Mapper
|
||||||
|
temperatureToHumidity []Mapper
|
||||||
|
humidityToLocation []Mapper
|
||||||
|
}
|
||||||
|
|
||||||
|
type Mapper struct {
|
||||||
|
source uint64
|
||||||
|
target uint64
|
||||||
|
count uint64
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user