2015 day 14
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
06a942483c
commit
ac4d282e1c
|
@ -19,6 +19,7 @@ public class Test2015
|
||||||
[DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")]
|
[DataRow(typeof(Day11), "hepxxyzz", "heqaabcc")]
|
||||||
[DataRow(typeof(Day12), "111754", "65402")]
|
[DataRow(typeof(Day12), "111754", "65402")]
|
||||||
[DataRow(typeof(Day13), "733", "725")]
|
[DataRow(typeof(Day13), "733", "725")]
|
||||||
|
[DataRow(typeof(Day14), "2655", "1059")]
|
||||||
public void CheckAllDays(Type dayType, string part1, string part2)
|
public void CheckAllDays(Type dayType, string part1, string part2)
|
||||||
{
|
{
|
||||||
Common.CheckDay(dayType, part1, part2);
|
Common.CheckDay(dayType, part1, part2);
|
||||||
|
|
|
@ -7,6 +7,9 @@ namespace AOC2015;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Day13 : Day
|
public sealed class Day13 : Day
|
||||||
{
|
{
|
||||||
|
private readonly Dictionary<(string person1, string person2), int> _happinessMap = new();
|
||||||
|
private readonly List<string> _people = new();
|
||||||
|
|
||||||
public Day13() : base(2015, 13, "Knights of the Dinner Table")
|
public Day13() : base(2015, 13, "Knights of the Dinner Table")
|
||||||
{
|
{
|
||||||
foreach (var line in Input)
|
foreach (var line in Input)
|
||||||
|
@ -21,40 +24,36 @@ public sealed class Day13 : Day
|
||||||
public override object Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
// Add myself
|
// Add myself
|
||||||
foreach (var person in allPeople)
|
foreach (var person in _people)
|
||||||
{
|
{
|
||||||
happinessMap[("me", person)] = 0;
|
_happinessMap[("me", person)] = 0;
|
||||||
happinessMap[(person, "me")] = 0;
|
_happinessMap[(person, "me")] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
allPeople.Add("me");
|
_people.Add("me");
|
||||||
|
|
||||||
return ComputeHappiness();
|
return ComputeHappiness();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<(string person1, string person2), int> happinessMap = new();
|
|
||||||
private List<string> allPeople = new();
|
|
||||||
|
|
||||||
private void AddToHappinessMap(string thisString)
|
private void AddToHappinessMap(string thisString)
|
||||||
{
|
{
|
||||||
var tokens = thisString.Split(' ');
|
var tokens = thisString.Split(' ');
|
||||||
var firstPerson = tokens.First();
|
var person1 = tokens.First();
|
||||||
var lastPerson = tokens.Last().TrimEnd('.');
|
var person2 = tokens.Last().TrimEnd('.');
|
||||||
var amount = int.Parse(tokens[3]);
|
var amount = int.Parse(tokens[3]);
|
||||||
|
|
||||||
if (tokens.Contains("lose"))
|
if (tokens.Contains("lose"))
|
||||||
amount *= -1;
|
amount *= -1;
|
||||||
|
|
||||||
happinessMap[(firstPerson, lastPerson)] = amount;
|
_happinessMap[(person1, person2)] = amount;
|
||||||
|
|
||||||
if (!allPeople.Contains(firstPerson))
|
if (!_people.Contains(person1))
|
||||||
allPeople.Add(firstPerson);
|
_people.Add(person1);
|
||||||
if (!allPeople.Contains(lastPerson))
|
if (!_people.Contains(person2))
|
||||||
allPeople.Add(lastPerson);
|
_people.Add(person2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<List<string>> BuildPermutations(List<string> items)
|
||||||
public static List<List<string>> BuildPermutations(List<string> items)
|
|
||||||
{
|
{
|
||||||
if (items.Count > 1)
|
if (items.Count > 1)
|
||||||
{
|
{
|
||||||
|
@ -67,7 +66,7 @@ public sealed class Day13 : Day
|
||||||
|
|
||||||
public long ComputeHappiness()
|
public long ComputeHappiness()
|
||||||
{
|
{
|
||||||
var possibilities = BuildPermutations(allPeople);
|
var possibilities = BuildPermutations(_people);
|
||||||
|
|
||||||
var maxHappiness = long.MinValue;
|
var maxHappiness = long.MinValue;
|
||||||
foreach (var possibility in possibilities)
|
foreach (var possibility in possibilities)
|
||||||
|
@ -81,8 +80,8 @@ public sealed class Day13 : Day
|
||||||
var leftPerson = i == 0 ? possibility[^1] : possibility[i - 1];
|
var leftPerson = i == 0 ? possibility[^1] : possibility[i - 1];
|
||||||
var rightPerson = i == possibility.Count - 1 ? possibility[0] : possibility[i + 1];
|
var rightPerson = i == possibility.Count - 1 ? possibility[0] : possibility[i + 1];
|
||||||
|
|
||||||
thisPossibilityHappiness += happinessMap[(firstPerson, leftPerson)];
|
thisPossibilityHappiness += _happinessMap[(firstPerson, leftPerson)];
|
||||||
thisPossibilityHappiness += happinessMap[(firstPerson, rightPerson)];
|
thisPossibilityHappiness += _happinessMap[(firstPerson, rightPerson)];
|
||||||
}
|
}
|
||||||
|
|
||||||
maxHappiness = Math.Max(maxHappiness, thisPossibilityHappiness);
|
maxHappiness = Math.Max(maxHappiness, thisPossibilityHappiness);
|
||||||
|
|
|
@ -5,11 +5,36 @@ namespace AOC2015;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Day14 : Day
|
public sealed class Day14 : Day
|
||||||
{
|
{
|
||||||
|
private readonly List<Reindeer> _reindeer;
|
||||||
|
|
||||||
public Day14() : base(2015, 14, "Reindeer Olympics")
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue