119 lines
3.5 KiB
C#
119 lines
3.5 KiB
C#
using Spectre.Console;
|
|
|
|
namespace AdventOfCode.Days;
|
|
|
|
public class Day7 : Day
|
|
{
|
|
public override int Number => 7;
|
|
public override string Name => "Bridge Repair";
|
|
|
|
public override void RunPart1(bool display = true)
|
|
{
|
|
var calibrationResult = 0L;
|
|
|
|
foreach (var line in Input.AsSpan().EnumerateLines())
|
|
{
|
|
// Read input equation
|
|
var equationStart = line.IndexOf(':');
|
|
|
|
var target = long.Parse(line[..equationStart]);
|
|
|
|
var numbers = new List<long>();
|
|
var equationNumbers = line[(equationStart + 2)..];
|
|
|
|
foreach (var range in equationNumbers.Split(' '))
|
|
{
|
|
numbers.Add(int.Parse(equationNumbers[range]));
|
|
}
|
|
|
|
// Check if equation is possible
|
|
if (CheckEquation(target, numbers.ToArray().AsSpan()[1..], numbers[0]))
|
|
{
|
|
calibrationResult += target;
|
|
}
|
|
}
|
|
|
|
if (display)
|
|
{
|
|
AnsiConsole.MarkupLine($"[green]Total calibration result: [yellow]{calibrationResult}[/][/]");
|
|
}
|
|
|
|
return;
|
|
|
|
bool CheckEquation(long target, ReadOnlySpan<long> numbers, long result)
|
|
{
|
|
// Early return when result is greater than target because result is increasing
|
|
if (result > target)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (numbers.IsEmpty)
|
|
{
|
|
return result == target;
|
|
}
|
|
|
|
var sumResult = result + numbers[0];
|
|
var multiplyResult = result * numbers[0];
|
|
|
|
return CheckEquation(target, numbers[1..], sumResult)
|
|
|| CheckEquation(target, numbers[1..], multiplyResult);
|
|
}
|
|
}
|
|
|
|
public override void RunPart2(bool display = true)
|
|
{
|
|
var calibrationResult = 0L;
|
|
|
|
foreach (var line in Input.AsSpan().EnumerateLines())
|
|
{
|
|
// Read input equation
|
|
var equationStart = line.IndexOf(':');
|
|
|
|
var target = long.Parse(line[..equationStart]);
|
|
|
|
var numbers = new List<long>();
|
|
var equationNumbers = line[(equationStart + 2)..];
|
|
|
|
foreach (var range in equationNumbers.Split(' '))
|
|
{
|
|
numbers.Add(int.Parse(equationNumbers[range]));
|
|
}
|
|
|
|
// Check if equation is possible
|
|
if (CheckEquation(target, numbers.ToArray().AsSpan()[1..], numbers[0]))
|
|
{
|
|
calibrationResult += target;
|
|
}
|
|
}
|
|
|
|
if (display)
|
|
{
|
|
AnsiConsole.MarkupLine($"[green]Total calibration result: [yellow]{calibrationResult}[/][/]");
|
|
}
|
|
|
|
return;
|
|
|
|
bool CheckEquation(long target, ReadOnlySpan<long> numbers, long result)
|
|
{
|
|
// Early return when result is greater than target because result is increasing
|
|
if (result > target)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (numbers.IsEmpty)
|
|
{
|
|
return result == target;
|
|
}
|
|
|
|
var sumResult = result + numbers[0];
|
|
var multiplyResult = result * numbers[0];
|
|
var concatResult = result * (int)Math.Pow(10, (int)Math.Log10(numbers[0]) + 1) + numbers[0];
|
|
|
|
return CheckEquation(target, numbers[1..], sumResult)
|
|
|| CheckEquation(target, numbers[1..], multiplyResult)
|
|
|| CheckEquation(target, numbers[1..], concatResult);
|
|
}
|
|
}
|
|
} |