diff --git a/day14/day.go b/day14/day.go new file mode 100644 index 0000000..6795188 --- /dev/null +++ b/day14/day.go @@ -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 +} diff --git a/inputs/14 b/inputs/14 new file mode 100644 index 0000000..dcd32c3 --- /dev/null +++ b/inputs/14 @@ -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. \ No newline at end of file diff --git a/inputs/14_test b/inputs/14_test new file mode 100644 index 0000000..5ec80e9 --- /dev/null +++ b/inputs/14_test @@ -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. \ No newline at end of file diff --git a/main.go b/main.go index f21ddb6..557abc3 100644 --- a/main.go +++ b/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}, } }