Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCodeDay14
{
class Program
{
static void Main(string[] args)
{
StreamReader ss = new StreamReader("../../input1.txt");
string line;
var input = new List<string>();
while ((line = ss.ReadLine()) != null)
{
input.Add(line);
}
var regex = new Regex("^(?<Name>.*) can fly (?<Speed>.*) km/s for (?<D1>.*) seconds, but then must rest for (?<D2>.*) seconds.$");
var reindeer = new List<Reindeer>();
foreach (var c in input)
{
var m = regex.Matches(c)[0];
var name = m.Groups["Name"].Value;
var speed = int.Parse(m.Groups["Speed"].Value);
var d1 = int.Parse(m.Groups["D1"].Value);
var d2 = int.Parse(m.Groups["D2"].Value);
reindeer.Add(new Reindeer
{
Name = name,
Speed = speed,
MovingDuration = d1,
RestingDuration = d2,
IsFlying = false,
NextStatusChange = 0,
Points = 0
});
}
const int raceLength = 2503;
for (var i = 0; i < raceLength; i++)
{
foreach (var r in reindeer)
{
if (r.NextStatusChange == 0)
{
if (r.IsFlying)
{
r.NextStatusChange = r.RestingDuration;
r.IsFlying = false;
}
else
{
r.NextStatusChange = r.MovingDuration;
r.IsFlying = true;
}
}
if (r.IsFlying)
{
r.TotalDistanceTraveled += r.Speed;
}
r.NextStatusChange--;
}
var leadDistance = reindeer.Max(a => a.TotalDistanceTraveled);
foreach (var leaders in reindeer.Where(a => a.TotalDistanceTraveled == leadDistance))
{
leaders.Points++;
}
}
reindeer = reindeer.OrderByDescending(a => a.Points).ToList();
reindeer.ForEach(a => Console.WriteLine($"{a.Name} : {a.TotalDistanceTraveled} : {a.Points}"));
Console.ReadLine();
}
public class Reindeer
{
public string Name { get; set; }
public int Speed { get; set; }
public int MovingDuration { get; set; }
public int RestingDuration { get; set; }
public bool IsFlying { get; set; }
public int NextStatusChange { get; set; }
public int TotalDistanceTraveled { get; set; }
public int Points { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.