ben
/
aoc
1
0
Fork 0

2015 day 13
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2022-12-04 22:53:30 -05:00
parent 048868fc4b
commit 06a942483c
3 changed files with 95 additions and 3 deletions

View File

@ -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);

View File

@ -1,3 +1,5 @@
//using MoreLinq.Extensions;
namespace AOC2015;
/// <summary>
@ -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<string> 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<List<string>> BuildPermutations(List<string> 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;
}
}

View File

@ -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.