From ac4d282e1cb0ebfe02c784293317650d099aa519 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sun, 4 Dec 2022 23:47:35 -0500 Subject: [PATCH] 2015 day 14 --- AOC.Test/Test2015.cs | 1 + AOC2015/Day13.cs | 39 +++++++++++++++++++-------------------- AOC2015/Day14.cs | 29 +++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 0ca488e..eec49db 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -19,6 +19,7 @@ public class Test2015 [DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")] [DataRow(typeof(Day12), "111754", "65402")] [DataRow(typeof(Day13), "733", "725")] + [DataRow(typeof(Day14), "2655", "1059")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); diff --git a/AOC2015/Day13.cs b/AOC2015/Day13.cs index 61e11ae..0341397 100644 --- a/AOC2015/Day13.cs +++ b/AOC2015/Day13.cs @@ -7,6 +7,9 @@ namespace AOC2015; /// public sealed class Day13 : Day { + private readonly Dictionary<(string person1, string person2), int> _happinessMap = new(); + private readonly List _people = new(); + public Day13() : base(2015, 13, "Knights of the Dinner Table") { foreach (var line in Input) @@ -21,40 +24,36 @@ public sealed class Day13 : Day public override object Part2() { // Add myself - foreach (var person in allPeople) + foreach (var person in _people) { - happinessMap[("me", person)] = 0; - happinessMap[(person, "me")] = 0; + _happinessMap[("me", person)] = 0; + _happinessMap[(person, "me")] = 0; } - - allPeople.Add("me"); + + _people.Add("me"); return ComputeHappiness(); } - private Dictionary<(string person1, string person2), int> happinessMap = new(); - private List allPeople = new(); - private void AddToHappinessMap(string thisString) { var tokens = thisString.Split(' '); - var firstPerson = tokens.First(); - var lastPerson = tokens.Last().TrimEnd('.'); + var person1 = tokens.First(); + var person2 = tokens.Last().TrimEnd('.'); var amount = int.Parse(tokens[3]); if (tokens.Contains("lose")) amount *= -1; - happinessMap[(firstPerson, lastPerson)] = amount; + _happinessMap[(person1, person2)] = amount; - if (!allPeople.Contains(firstPerson)) - allPeople.Add(firstPerson); - if (!allPeople.Contains(lastPerson)) - allPeople.Add(lastPerson); + if (!_people.Contains(person1)) + _people.Add(person1); + if (!_people.Contains(person2)) + _people.Add(person2); } - - public static List> BuildPermutations(List items) + private static List> BuildPermutations(List items) { if (items.Count > 1) { @@ -67,7 +66,7 @@ public sealed class Day13 : Day public long ComputeHappiness() { - var possibilities = BuildPermutations(allPeople); + var possibilities = BuildPermutations(_people); var maxHappiness = long.MinValue; foreach (var possibility in possibilities) @@ -81,8 +80,8 @@ public sealed class Day13 : Day var leftPerson = i == 0 ? possibility[^1] : possibility[i - 1]; var rightPerson = i == possibility.Count - 1 ? possibility[0] : possibility[i + 1]; - thisPossibilityHappiness += happinessMap[(firstPerson, leftPerson)]; - thisPossibilityHappiness += happinessMap[(firstPerson, rightPerson)]; + thisPossibilityHappiness += _happinessMap[(firstPerson, leftPerson)]; + thisPossibilityHappiness += _happinessMap[(firstPerson, rightPerson)]; } maxHappiness = Math.Max(maxHappiness, thisPossibilityHappiness); diff --git a/AOC2015/Day14.cs b/AOC2015/Day14.cs index 238ca59..83f8056 100644 --- a/AOC2015/Day14.cs +++ b/AOC2015/Day14.cs @@ -5,11 +5,36 @@ namespace AOC2015; /// public sealed class Day14 : Day { + private readonly List _reindeer; + public Day14() : base(2015, 14, "Reindeer Olympics") { + _reindeer = Input.Select(i => new Reindeer(i)).ToList(); } - public override object Part1() => ""; + public override object Part1() => + _reindeer.Select(r => r.Fly(2503)).Max(); - public override object Part2() => ""; + public override object Part2() => + Enumerable.Range(1, 2503) + .SelectMany(time => _reindeer.GroupBy(r => r.Fly(time)).OrderByDescending(r => r.Key).First()) + .GroupBy(r => r) + .Max(g => g.Count()); + + private class Reindeer { + private readonly int _duration; + private readonly int _rest; + private readonly int _speed; + + public Reindeer(string input) { + var p = input.Split(' '); + _speed = int.Parse(p[3]); + _duration = int.Parse(p[6]); + _rest = int.Parse(p[13]); + } + + public int Fly(int time) => + time / (_duration + _rest) * _speed * _duration // number iterations * travel distance + + Math.Min(_duration, time % (_duration + _rest)) * _speed; // last partial iteration + } }