aoc2015/day14/day.go
Stefan Harmuth 8c2f62a2d5 day14
2020-12-27 21:24:18 +01:00

81 lines
2.1 KiB
Go

package day14
import (
"strconv"
"strings"
"tools"
)
func Part1(puzzle tools.AoCPuzzle) interface{} {
seconds := 2503
maxDistance := 0
for _, line := range puzzle.GetInputArray() {
parts := strings.Split(line, " ")
speed, _ := strconv.Atoi(parts[3])
timeFlying, _ := strconv.Atoi(parts[6])
timeResting, _ := strconv.Atoi(parts[13])
distanceMultiplier := seconds / (timeFlying + timeResting)
distanceRemainder := seconds % (timeFlying + timeResting)
distance := distanceMultiplier * timeFlying * speed
if distanceRemainder > timeFlying {
distance += speed * timeFlying
} else {
distance += speed * distanceRemainder
}
if distance > maxDistance {
maxDistance = distance
}
}
return maxDistance
}
func Part2(puzzle tools.AoCPuzzle) interface{} {
seconds := 2503
config := make(map[string]map[string]int)
for _, line := range puzzle.GetInputArray() {
parts := strings.Split(line, " ")
config[parts[0]] = make(map[string]int)
config[parts[0]]["speed"], _ = strconv.Atoi(parts[3])
config[parts[0]]["timeFlying"], _ = strconv.Atoi(parts[6])
config[parts[0]]["timeResting"], _ = strconv.Atoi(parts[13])
}
points := make(map[string]int)
var maxDistance int
currentDistance := make(map[string]int)
for i := 1; i <= seconds; i++ {
maxDistance = 0
for reindeer, reindeerConfig := range config {
distanceMultiplier := i / (reindeerConfig["timeFlying"] + reindeerConfig["timeResting"])
distanceRemainder := i % (reindeerConfig["timeFlying"] + reindeerConfig["timeResting"])
distance := distanceMultiplier * reindeerConfig["timeFlying"] * reindeerConfig["speed"]
if distanceRemainder > reindeerConfig["timeFlying"] {
distance += reindeerConfig["speed"] * reindeerConfig["timeFlying"]
} else {
distance += reindeerConfig["speed"] * distanceRemainder
}
currentDistance[reindeer] = distance
if distance > maxDistance {
maxDistance = distance
}
}
for reindeer, distance := range currentDistance {
if distance == maxDistance {
points[reindeer]++
}
}
}
var maxPoints int
for _, accPoints := range points {
if accPoints > maxPoints {
maxPoints = accPoints
}
}
return maxPoints
}