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)..]) ); } }