day14
This commit is contained in:
parent
13428cc28a
commit
8c2f62a2d5
80
day14/day.go
Normal file
80
day14/day.go
Normal file
@ -0,0 +1,80 @@
|
||||
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
|
||||
}
|
||||
9
inputs/14
Normal file
9
inputs/14
Normal file
@ -0,0 +1,9 @@
|
||||
Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds.
|
||||
Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds.
|
||||
Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.
|
||||
Donner can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
|
||||
Dasher can fly 11 km/s for 12 seconds, but then must rest for 125 seconds.
|
||||
Comet can fly 21 km/s for 6 seconds, but then must rest for 121 seconds.
|
||||
Blitzen can fly 18 km/s for 3 seconds, but then must rest for 50 seconds.
|
||||
Vixen can fly 20 km/s for 4 seconds, but then must rest for 75 seconds.
|
||||
Dancer can fly 7 km/s for 20 seconds, but then must rest for 119 seconds.
|
||||
2
inputs/14_test
Normal file
2
inputs/14_test
Normal file
@ -0,0 +1,2 @@
|
||||
Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
|
||||
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.
|
||||
2
main.go
2
main.go
@ -14,6 +14,7 @@ import (
|
||||
"aoc2015/day11"
|
||||
"aoc2015/day12"
|
||||
"aoc2015/day13"
|
||||
"aoc2015/day14"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -44,6 +45,7 @@ func initDayFunctions() {
|
||||
11: {1: day11.Part1, 2: day11.Part2},
|
||||
12: {1: day12.Part1, 2: day12.Part2},
|
||||
13: {1: day13.Part1, 2: day13.Part2},
|
||||
14: {1: day14.Part1, 2: day14.Part2},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user