2022-12-03 05:55:49 +00:00
|
|
|
namespace AOC2015;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-12-03 05:41:38 +00:00
|
|
|
/// Day 15: <a href="https://adventofcode.com/2015/day/15"/>
|
2022-11-11 18:42:42 +00:00
|
|
|
/// </summary>
|
2022-12-05 23:06:47 +00:00
|
|
|
public sealed partial class Day15 : Day
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
private int _best;
|
|
|
|
private int _best500Cals;
|
|
|
|
|
|
|
|
public Day15() : base(2015, 15, "Science for Hungry People")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:06:47 +00:00
|
|
|
[GeneratedRegex(@"-?\d+")]
|
|
|
|
private static partial Regex Digits();
|
|
|
|
|
2022-12-08 19:40:40 +00:00
|
|
|
private static Ingredient ParseIngredient(string line)
|
|
|
|
{
|
2022-12-13 19:58:05 +00:00
|
|
|
var nums = Digits().Matches(line.Split(':')[1]).Select(match => int.Parse(match.Value)).ToList();
|
|
|
|
return new(nums[0], nums[1], nums[2], nums[3], nums[4]);
|
2022-12-08 19:40:40 +00:00
|
|
|
}
|
2022-12-05 23:06:47 +00:00
|
|
|
|
2022-12-08 19:40:40 +00:00
|
|
|
public override void ProcessInput()
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
var ingredients = Input.Select(ParseIngredient).ToList();
|
|
|
|
var quantities = new int[ingredients.Count];
|
2022-12-05 23:06:47 +00:00
|
|
|
_best = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
for (var i = 0; i < ingredients.Count; i++)
|
2022-12-05 23:06:47 +00:00
|
|
|
{
|
|
|
|
quantities[i]++;
|
|
|
|
if (quantities[i] > 100)
|
|
|
|
quantities[i] = 0;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:40:40 +00:00
|
|
|
var quantityApplied = quantities.Take(ingredients.Count - 1).Sum();
|
2022-12-05 23:06:47 +00:00
|
|
|
if (quantityApplied == 0) break;
|
|
|
|
if (quantityApplied > 100) continue;
|
|
|
|
|
|
|
|
quantities[^1] = 100 - quantityApplied;
|
|
|
|
|
|
|
|
int cap = 0, dur = 0, fla = 0, tex = 0, cal = 0;
|
2022-12-08 19:40:40 +00:00
|
|
|
for (var j = 0; j < ingredients.Count; j++)
|
2022-12-05 23:06:47 +00:00
|
|
|
{
|
|
|
|
var q = quantities[j];
|
2022-12-08 19:40:40 +00:00
|
|
|
var i = ingredients[j];
|
2022-12-05 23:06:47 +00:00
|
|
|
|
|
|
|
cap += q * i.Capacity;
|
|
|
|
dur += q * i.Durability;
|
|
|
|
fla += q * i.Flavor;
|
|
|
|
tex += q * i.Texture;
|
|
|
|
cal += q * i.Calories;
|
|
|
|
}
|
|
|
|
|
|
|
|
var total = Math.Max(0, cap) * Math.Max(0, dur) * Math.Max(0, fla) * Math.Max(0, tex);
|
|
|
|
|
|
|
|
if (total > _best) _best = total;
|
|
|
|
if (total > _best500Cals && cal == 500) _best500Cals = total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object Part1() => _best;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
2022-12-05 23:06:47 +00:00
|
|
|
public override object Part2() => _best500Cals;
|
2022-12-08 19:40:40 +00:00
|
|
|
|
2022-12-13 19:58:05 +00:00
|
|
|
private record Ingredient(int Capacity, int Durability, int Flavor, int Texture, int Calories);
|
2022-11-11 18:42:42 +00:00
|
|
|
}
|