feat: solve day 7 part 1 and part 2
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
namespace BridgeRepair;
|
||||
|
||||
// TODO: Look at what the public API should be here.
|
||||
class Equation(long testValue, List<long> numbers)
|
||||
{
|
||||
public readonly long TestValue = testValue;
|
||||
|
||||
public bool IsPossible(bool isPart2 = false)
|
||||
{
|
||||
var configs = GeneratePossibleOperatorConfigurations(isPart2).ToList();
|
||||
return configs.Select(Evaluate).Any(result => result == TestValue);
|
||||
}
|
||||
|
||||
public long Evaluate(char[] operators)
|
||||
{
|
||||
var result = numbers[0];
|
||||
|
||||
for (var i = 0; i < operators.Length; i++)
|
||||
{
|
||||
var currentOperator = operators[i];
|
||||
var number = numbers[i + 1];
|
||||
|
||||
if (currentOperator is '+')
|
||||
{
|
||||
result += number;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (currentOperator is '*')
|
||||
{
|
||||
result *= number;
|
||||
continue;
|
||||
}
|
||||
|
||||
result = long.Parse(result + number.ToString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public IEnumerable<char[]> GeneratePossibleOperatorConfigurations(bool isPart2 = false)
|
||||
{
|
||||
// TODO: make it possible to pass is operators we can use.
|
||||
var numberOfPositions = numbers.Count - 1;
|
||||
var config = new char[numberOfPositions];
|
||||
var numOfOperators = isPart2 ? 3 : 2;
|
||||
var numberOfPossibleConfigurations = Math.Pow(numOfOperators, numberOfPositions);
|
||||
|
||||
for (var i = 0; i < numberOfPossibleConfigurations; i++)
|
||||
{
|
||||
var j = 0;
|
||||
|
||||
while (j < numberOfPositions)
|
||||
{
|
||||
var currentOperator = config[j];
|
||||
|
||||
if (currentOperator is '+')
|
||||
{
|
||||
config[j] = '*';
|
||||
break;
|
||||
}
|
||||
|
||||
if (isPart2 && currentOperator is '*')
|
||||
{
|
||||
config[j] = '|';
|
||||
break;
|
||||
}
|
||||
|
||||
config[j] = '+';
|
||||
j++;
|
||||
}
|
||||
|
||||
yield return config.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using BridgeRepair;
|
||||
|
||||
if (args.Length is 0)
|
||||
{
|
||||
Console.WriteLine("Please provide a path to the input file.");
|
||||
@@ -18,7 +20,9 @@ var input = await File.ReadAllLinesAsync(args[0]);
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
// TODO: Implement solution
|
||||
var result = new PuzzleParser().Parse(input)
|
||||
.Where(e => e.IsPossible(isPart2))
|
||||
.Sum(e => e.TestValue);
|
||||
|
||||
stopwatch.Stop();
|
||||
Console.WriteLine($". ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
Console.WriteLine($"The sum of the test value for possible equations is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace BridgeRepair;
|
||||
|
||||
class PuzzleParser
|
||||
{
|
||||
public List<Equation> Parse(string[] input)
|
||||
{
|
||||
return input.Select(s => s.ToEquation()).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace BridgeRepair;
|
||||
|
||||
static class StringExtensions
|
||||
{
|
||||
public static Equation ToEquation(this string line)
|
||||
{
|
||||
var parts = line.Split(':');
|
||||
var testValue = long.Parse(parts[0]);
|
||||
var numbers = parts[1].Split(" ", StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(long.Parse)
|
||||
.ToList();
|
||||
return new(testValue, numbers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user