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.
Read the full puzzle.
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace AdventOfCode.Y2015.Day14;
[ProblemName("Reindeer Olympics")]
class Solution : Solver {
public object PartOne(string input) => Race(Parse(input)).Skip(2502).First().Max();
public object PartTwo(string input) => Race2(Parse(input)).Skip(2502).First().Max();
IEnumerable<int>[] Parse(string input) => input.Split('\n').Select(Reindeer).ToArray();
IEnumerable<int[]> Race(IEnumerable<int>[] reindeers) {
var res = new int[reindeers.Length];
var enumarators = reindeers.Select(r => r.GetEnumerator()).ToArray();
while (true) {
yield return (from en in enumarators
let _ = en.MoveNext()
select en.Current).ToArray();
}
}
IEnumerable<int[]> Race2(IEnumerable<int>[] reindeers) {
var points = new int[reindeers.Length];
foreach (var step in Race(reindeers)) {
var m = step.Max();
for (var i = 0; i < step.Length; i++) {
if (step[i] == m) {
points[i]++;
}
}
yield return points;
}
}
IEnumerable<int> Reindeer(string line) {
var m = Regex.Match(line, @"(.*) can fly (.*) km/s for (.*) seconds, but then must rest for (.*) seconds.");
var speed = int.Parse(m.Groups[2].Value);
var flightTime = int.Parse(m.Groups[3].Value);
var restTime = int.Parse(m.Groups[4].Value);
var t = 0;
var dist = 0;
var flying = true;
while (true) {
if (flying) {
dist += speed;
}
t++;
if ((flying && t == flightTime) || (!flying && t == restTime)) {
t = 0;
flying = !flying;
}
yield return dist;
}
}
}
Please ☆ my repo if you like it!