Add day 13

This commit is contained in:
2025-01-24 10:19:12 +01:00
parent 5f08adc3cb
commit 10429f3302
2 changed files with 1366 additions and 0 deletions

87
Days/Day13.cs Normal file
View File

@@ -0,0 +1,87 @@
using System.Collections.Frozen;
using System.Numerics;
using System.Text;
using Spectre.Console;
namespace AdventOfCode.Days;
public class Day13 : Day
{
public override int Number => 13;
public override string Name => "Claw Contraption";
private const int MaxButtonPress = 100;
private const int ButtonACost = 3;
private const int ButtonBCost = 1;
public override void RunPart1(bool display = true)
{
var lines = Input.AsSpan().EnumerateLines();
var minimumTokensToWin = 0;
while (lines.MoveNext())
{
var (aX, aY, bX, bY, goalX, goalY) = ParseMachine(ref lines);
for (var i = MaxButtonPress; i >= 0; i--)
{
var xOffset = bX * i;
if (xOffset > goalX)
{
continue;
}
if ((goalX - xOffset) % aX != 0)
{
continue;
}
var timesAPressed = (goalX - xOffset) / aX;
if (bY * i + aY * timesAPressed != goalY)
{
continue;
}
minimumTokensToWin += i * ButtonBCost + timesAPressed * ButtonACost;
break;
}
}
if (display)
{
AnsiConsole.MarkupLine($"[green]Minimum tokens to win all prizes: [yellow]{minimumTokensToWin}[/][/]");
}
}
public override void RunPart2(bool display = true)
{
}
private (int aX, int aY, int bX, int bY, int goalX, int goalY) ParseMachine(ref SpanLineEnumerator lines)
{
var lineA = lines.Current;
lines.MoveNext();
var lineB = lines.Current;
lines.MoveNext();
var lineGoal = lines.Current;
lines.MoveNext();
return (
int.Parse(lineA[(lineA.IndexOf('+') + 1)..lineA.IndexOf(',')]),
int.Parse(lineA[(lineA.LastIndexOf('+') + 1)..]),
int.Parse(lineB[(lineB.IndexOf('+') + 1)..lineB.IndexOf(',')]),
int.Parse(lineB[(lineB.LastIndexOf('+') + 1)..]),
int.Parse(lineGoal[(lineGoal.IndexOf('=') + 1)..lineGoal.IndexOf(',')]),
int.Parse(lineGoal[(lineGoal.LastIndexOf('=') + 1)..])
);
}
}

1279
Inputs/Day13.txt Normal file

File diff suppressed because it is too large Load Diff