add a few more solutions
This commit is contained in:
14
Level7/Level7.csproj
Normal file
14
Level7/Level7.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../Common/Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
Level7/Level7.sln
Normal file
25
Level7/Level7.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.002.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Level7", "Level7.csproj", "{B69CB5AF-BF4D-4425-A20B-3E95D2E0EABB}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B69CB5AF-BF4D-4425-A20B-3E95D2E0EABB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B69CB5AF-BF4D-4425-A20B-3E95D2E0EABB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B69CB5AF-BF4D-4425-A20B-3E95D2E0EABB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B69CB5AF-BF4D-4425-A20B-3E95D2E0EABB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {3F192730-CD0E-42EA-89C1-94B84247D113}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
239
Level7/Level7Solver.cs
Normal file
239
Level7/Level7Solver.cs
Normal file
@@ -0,0 +1,239 @@
|
||||
namespace AoC24;
|
||||
|
||||
using AoC24.Common;
|
||||
using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Data;
|
||||
using Parsing.Schema;
|
||||
|
||||
public enum Operator
|
||||
{
|
||||
Multiply,
|
||||
Add,
|
||||
Concatenate,
|
||||
}
|
||||
|
||||
public class Equation
|
||||
{
|
||||
private List<int> operands;
|
||||
|
||||
private List<Operator> operators;
|
||||
|
||||
public Equation(List<int> operands)
|
||||
{
|
||||
this.operands = new List<int>();
|
||||
this.operands.AddRange(operands);
|
||||
this.operators = new List<Operator>();
|
||||
}
|
||||
|
||||
private Equation(List<int> operands, List<Operator> operators)
|
||||
{
|
||||
this.operands = new List<int>();
|
||||
this.operands.AddRange(operands);
|
||||
this.operators = new List<Operator>();
|
||||
this.operators.AddRange(operators);
|
||||
}
|
||||
|
||||
public void AddOperator(Operator op)
|
||||
{
|
||||
this.operators.Add(op);
|
||||
}
|
||||
|
||||
public bool CanHoldMoreOperators()
|
||||
{
|
||||
return this.operators.Count < (this.operands.Count - 1);
|
||||
}
|
||||
|
||||
public long Solve()
|
||||
{
|
||||
long result = this.operands[0];
|
||||
for(int i = 0; i < this.operators.Count; i++)
|
||||
{
|
||||
switch(this.operators[i])
|
||||
{
|
||||
case Operator.Add:
|
||||
result = result + (this.operands[i+1]);
|
||||
break;
|
||||
case Operator.Multiply:
|
||||
result = result * (this.operands[i+1]);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("This should never be reached");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public long Solve2()
|
||||
{
|
||||
long result = this.operands[0];
|
||||
for(int i = 0; i < this.operators.Count; i++)
|
||||
{
|
||||
switch(this.operators[i])
|
||||
{
|
||||
case Operator.Add:
|
||||
result = result + (this.operands[i+1]);
|
||||
break;
|
||||
case Operator.Multiply:
|
||||
result = result * (this.operands[i+1]);
|
||||
break;
|
||||
case Operator.Concatenate:
|
||||
result = result * (long)Math.Pow(10, (this.operands[i+1].ToString().Length)) + this.operands[i+1];
|
||||
break;
|
||||
default:
|
||||
throw new Exception("This should never be reached");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Equation Clone()
|
||||
{
|
||||
return new Equation(this.operands, this.operators);
|
||||
}
|
||||
}
|
||||
|
||||
public class EquationBuilder
|
||||
{
|
||||
public long TestValue;
|
||||
|
||||
private List<int> operands;
|
||||
|
||||
private List<Operator> operatorOptions;
|
||||
|
||||
private List<Equation> SolvableEquations = new List<Equation>();
|
||||
|
||||
public EquationBuilder(long testValue, params Operator[] operatorOptions)
|
||||
{
|
||||
this.TestValue = testValue;
|
||||
this.operands = new List<int>();
|
||||
this.operatorOptions = new List<Operator>();
|
||||
this.operatorOptions.AddRange(operatorOptions);
|
||||
}
|
||||
|
||||
public void AddOperand(int operand)
|
||||
{
|
||||
this.operands.Add(operand);
|
||||
}
|
||||
|
||||
private List<Equation> BuildEquationCandidates(Equation e)
|
||||
{
|
||||
List<Equation> resultEquations = new List<Equation>();
|
||||
if(!e.CanHoldMoreOperators())
|
||||
{
|
||||
resultEquations.Add(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach(var op in this.operatorOptions)
|
||||
{
|
||||
var newEquation = e.Clone();
|
||||
newEquation.AddOperator(op);
|
||||
resultEquations.AddRange(BuildEquationCandidates(newEquation));
|
||||
}
|
||||
}
|
||||
return resultEquations;
|
||||
}
|
||||
|
||||
public bool FindSolutions()
|
||||
{
|
||||
var allPossibleEquations = BuildEquationCandidates(new Equation(this.operands));
|
||||
foreach(var equation in allPossibleEquations)
|
||||
{
|
||||
if(equation.Solve() == this.TestValue)
|
||||
{
|
||||
this.SolvableEquations.Add(equation);
|
||||
}
|
||||
}
|
||||
return this.SolvableEquations.Any();
|
||||
}
|
||||
|
||||
public bool FindSolutions2()
|
||||
{
|
||||
var allPossibleEquations = BuildEquationCandidates(new Equation(this.operands));
|
||||
foreach(var equation in allPossibleEquations)
|
||||
{
|
||||
if(equation.Solve2() == this.TestValue)
|
||||
{
|
||||
this.SolvableEquations.Add(equation);
|
||||
}
|
||||
}
|
||||
return this.SolvableEquations.Any();
|
||||
}
|
||||
}
|
||||
|
||||
public class Level7Solver : FullTextLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
get { return 7; }
|
||||
}
|
||||
|
||||
private long ParseLong(string word)
|
||||
{
|
||||
return long.Parse(word.Replace(":", ""));
|
||||
}
|
||||
|
||||
protected override InputSchemaBuilder DefineInputSchema(InputSchemaBuilder schemaBuilder)
|
||||
{
|
||||
return schemaBuilder
|
||||
.Expect(InputType.Custom, InputType.Long, this.ParseLong)
|
||||
.Repeat()
|
||||
.Expect(InputType.Long)
|
||||
.EndRepetition();
|
||||
}
|
||||
|
||||
public override string SolveFirstStar()
|
||||
{
|
||||
var equationBuilders = this.GetData()
|
||||
.Filter(InputType.Long)
|
||||
.AsListRows<long>()
|
||||
.TransformData((List<long> inputs) => {
|
||||
var eb = new EquationBuilder(inputs[0], Operator.Multiply, Operator.Add);
|
||||
for(int i = 1; i < inputs.Count; i++)
|
||||
{
|
||||
eb.AddOperand((int)inputs[i]);
|
||||
}
|
||||
return eb;
|
||||
});
|
||||
|
||||
long totalSum = 0;
|
||||
|
||||
foreach(var equationBuilder in equationBuilders)
|
||||
{
|
||||
if(equationBuilder.FindSolutions())
|
||||
{
|
||||
totalSum += equationBuilder.TestValue;
|
||||
}
|
||||
}
|
||||
|
||||
return totalSum.ToString();
|
||||
}
|
||||
|
||||
public override string SolveSecondStar()
|
||||
{
|
||||
var equationBuilders = this.GetData()
|
||||
.Filter(InputType.Long)
|
||||
.AsListRows<long>()
|
||||
.TransformData((List<long> inputs) => {
|
||||
var eb = new EquationBuilder(inputs[0], Operator.Multiply, Operator.Add, Operator.Concatenate);
|
||||
for(int i = 1; i < inputs.Count; i++)
|
||||
{
|
||||
eb.AddOperand((int)inputs[i]);
|
||||
}
|
||||
return eb;
|
||||
});
|
||||
|
||||
long totalSum = 0;
|
||||
|
||||
foreach(var equationBuilder in equationBuilders)
|
||||
{
|
||||
if(equationBuilder.FindSolutions2())
|
||||
{
|
||||
totalSum += equationBuilder.TestValue;
|
||||
}
|
||||
}
|
||||
|
||||
return totalSum.ToString();
|
||||
}
|
||||
}
|
||||
24
Level7/Program.cs
Normal file
24
Level7/Program.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using AoC24;
|
||||
|
||||
var levelSolver = new Level7Solver();
|
||||
var solution1 = levelSolver.SolveFirstStar();
|
||||
var solution2 = levelSolver.SolveSecondStar();
|
||||
|
||||
if (!string.IsNullOrEmpty(solution1))
|
||||
{
|
||||
Console.WriteLine("Solution for example 1 is: " + solution1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Example 1 has not been solved yet!");
|
||||
}
|
||||
if (!string.IsNullOrEmpty(solution2))
|
||||
{
|
||||
Console.WriteLine("Solution for example 2 is: " + solution2);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Example 2 has not been solved yet!");
|
||||
}
|
||||
Reference in New Issue
Block a user