/**
 * Created by tomwallace on 12/7/15.
 */
class DayFourteenService {

    final static Integer PUZZLE_TIME = 2503
    final static String PUZZLE_INPUT = """Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds.
Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds.
Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds.
Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds.
Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds.
Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds.
Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds.
Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds.
Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds.
"""

    // http://adventofcode.com/day/14

    /*
This year is the Reindeer Olympics! Reindeer can fly at high speeds, but must rest occasionally to recover their energy. Santa would like to know which of his reindeer is fastest, and so he has them race.

Reindeer can only either be flying (always at their top speed) or resting (not moving at all), and always spend whole seconds in either state.

For example, suppose you have the following Reindeer:

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.
After one second, Comet has gone 14 km, while Dancer has gone 16 km. After ten seconds, Comet has gone 140 km, while Dancer has gone 160 km. On the eleventh second, Comet begins resting (staying at 140 km), and Dancer continues on for a total distance of 176 km. On the 12th second, both reindeer are resting. They continue to rest until the 138th second, when Comet flies for another ten seconds. On the 174th second, Dancer flies for another 11 seconds.

In this example, after the 1000th second, both reindeer are resting, and Comet is in the lead at 1120 km (poor Dancer has only gotten 1056 km by that point). So, in this situation, Comet would win (if the race ended at 1000 seconds).

Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled?

     */
    List<Integer> furthestDeerDistance(Map speedData, Integer time) {
        List<Integer> dists = []
        speedData.each { data ->
            Integer result = deerDistanceCovered(data.value, time)
            dists << result
        }

        return dists
    }

    /*
PART 2 - Seeing how reindeer move in bursts, Santa decides he's not pleased with the old scoring system.

Instead, at the end of each second, he awards one point to the reindeer currently in the lead. (If there are multiple reindeer tied for the lead, they each get one point.) He keeps the traditional 2503 second time limit, of course, as doing otherwise would be entirely ridiculous.

Given the example reindeer from above, after the first second, Dancer is in the lead and gets one point. He stays in the lead until several seconds into Comet's second burst: after the 140th second, Comet pulls into the lead and gets his first point. Of course, since Dancer had been in the lead for the 139 seconds before that, he has accumulated 139 points by the 140th second.

After the 1000th second, Dancer has accumulated 689 points, while poor Comet, our old champion, only has 312. So, with the new scoring system, Dancer would win (if the race ended at 1000 seconds).

Again given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, how many points does the winning reindeer have?
     */
    Integer mostPoints(String input, Integer time) {
        Map speedData = createMapOfSpeeds(input)
        List<Integer> points = [0,0,0,0,0,0,0,0,0]
        def i
        for (i = 1; i <= time; i++) {
            List<Integer> roundResults = furthestDeerDistance(speedData, i)
            Integer most = roundResults.sort(false) { -it }[0]
            Integer index = roundResults.findIndexOf { it == most }
            points[index] = points[index] + 1
        }

        return points.sort { -it }[0]
    }

    protected Integer deerDistanceCovered(Map data, Integer time) {
        Double ratio = (time / (data.duration.toInteger() + data.rest.toInteger()))
        Integer adjRatio = ratio.trunc()
        Integer remainder = time - ((data.duration.toInteger() + data.rest.toInteger()) * adjRatio)
        Integer adjRemainder = remainder > data.duration.toInteger() ? data.duration.toInteger() : remainder
        Integer distCovered = (adjRatio * data.speed.toInteger() * data.duration.toInteger()) + (adjRemainder * data.speed.toInteger())
        return distCovered
    }

    protected Map createMapOfSpeeds(String input) {
        // Create our map of distances
        Map data = [:]
        input.tokenize("\n").each { String line ->
            String deer = line.find(/^(\w*)\b/)
            List<Integer> nums = line.findAll(/(\d+)/)
            Map deerData = [speed: nums[0], duration: nums[1], rest: nums[2]]
            data["${deer}"] = deerData
        }

        return data
    }
}
