From 06a942483cf315422eab2f73fb1fdde069160af6 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sun, 4 Dec 2022 22:53:30 -0500 Subject: [PATCH] 2015 day 13 --- AOC.Test/Test2015.cs | 2 + AOC2015/Day13.cs | 84 +++++++++++++++++++++++++++++++++++-- AOC2015/input2015/test13.in | 12 ++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 AOC2015/input2015/test13.in diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 672ae51..0ca488e 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -18,6 +18,7 @@ public class Test2015 [DataRow(typeof(Day10), "492982", "6989950")] [DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")] [DataRow(typeof(Day12), "111754", "65402")] + [DataRow(typeof(Day13), "733", "725")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -34,6 +35,7 @@ public class Test2015 [DataRow(typeof(Day08), "12", "19")] [DataRow(typeof(Day09), "605", "982")] [DataRow(typeof(Day10), "237746", "3369156")] + [DataRow(typeof(Day13), "330", "286")] public void CheckTestInputs(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2, true); diff --git a/AOC2015/Day13.cs b/AOC2015/Day13.cs index a01cb89..61e11ae 100644 --- a/AOC2015/Day13.cs +++ b/AOC2015/Day13.cs @@ -1,3 +1,5 @@ +//using MoreLinq.Extensions; + namespace AOC2015; /// @@ -7,9 +9,85 @@ public sealed class Day13 : Day { public Day13() : base(2015, 13, "Knights of the Dinner Table") { + foreach (var line in Input) + AddToHappinessMap(line); } - public override object Part1() => ""; + public override object Part1() + { + return ComputeHappiness(); + } - public override object Part2() => ""; -} + public override object Part2() + { + // Add myself + foreach (var person in allPeople) + { + happinessMap[("me", person)] = 0; + happinessMap[(person, "me")] = 0; + } + + allPeople.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 amount = int.Parse(tokens[3]); + + if (tokens.Contains("lose")) + amount *= -1; + + happinessMap[(firstPerson, lastPerson)] = amount; + + if (!allPeople.Contains(firstPerson)) + allPeople.Add(firstPerson); + if (!allPeople.Contains(lastPerson)) + allPeople.Add(lastPerson); + } + + + public static List> BuildPermutations(List items) + { + if (items.Count > 1) + { + return items.SelectMany(item => BuildPermutations(items.Where(i => !i.Equals(item)).ToList()), + (item, permutation) => new[] { item }.Concat(permutation).ToList()).ToList(); + } + + return new() { items }; + } + + public long ComputeHappiness() + { + var possibilities = BuildPermutations(allPeople); + + var maxHappiness = long.MinValue; + foreach (var possibility in possibilities) + { + long thisPossibilityHappiness = 0; + + for (var i = 0; i < possibility.Count; i++) + { + // get the people on either side. + var firstPerson = possibility[i]; + 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)]; + } + + maxHappiness = Math.Max(maxHappiness, thisPossibilityHappiness); + } + + return maxHappiness; + } +} \ No newline at end of file diff --git a/AOC2015/input2015/test13.in b/AOC2015/input2015/test13.in new file mode 100644 index 0000000..b67c3c6 --- /dev/null +++ b/AOC2015/input2015/test13.in @@ -0,0 +1,12 @@ +Alice would gain 54 happiness units by sitting next to Bob. +Alice would lose 79 happiness units by sitting next to Carol. +Alice would lose 2 happiness units by sitting next to David. +Bob would gain 83 happiness units by sitting next to Alice. +Bob would lose 7 happiness units by sitting next to Carol. +Bob would lose 63 happiness units by sitting next to David. +Carol would lose 62 happiness units by sitting next to Alice. +Carol would gain 60 happiness units by sitting next to Bob. +Carol would gain 55 happiness units by sitting next to David. +David would gain 46 happiness units by sitting next to Alice. +David would lose 7 happiness units by sitting next to Bob. +David would gain 41 happiness units by sitting next to Carol.