add level 3 and backport textparser changes
This commit is contained in:
parent
377ef9afe8
commit
5b9ad5766b
37
Common/FragmentLevelSolverBase.cs
Normal file
37
Common/FragmentLevelSolverBase.cs
Normal file
@ -0,0 +1,37 @@
|
||||
namespace AoC24.Common;
|
||||
|
||||
using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public abstract class FragmentLevelSolverBase
|
||||
{
|
||||
public abstract int LevelNumber { get; }
|
||||
private int Year = 2024;
|
||||
private LevelInputProvider inputProvider = new LevelInputProvider();
|
||||
|
||||
protected string GetLevelInput()
|
||||
{
|
||||
return inputProvider.WithYear(this.Year).WithLevel(this.LevelNumber).Provide();
|
||||
}
|
||||
|
||||
public abstract string SolveFirstStar();
|
||||
|
||||
public abstract string SolveSecondStar();
|
||||
|
||||
protected abstract FragmentSchemaBuilder DefineInputSchema(FragmentSchemaBuilder schemaBuilder);
|
||||
|
||||
public TokenConverter GetData()
|
||||
{
|
||||
FragmentSchemaBuilder schemaBuilder = new FragmentSchemaBuilder();
|
||||
var schema = this.DefineInputSchema(schemaBuilder).Build();
|
||||
|
||||
var parser = TextParser.Create(schema);
|
||||
var data = parser
|
||||
.SetInputText(this.GetLevelInput())
|
||||
.Parse();
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using Parsing;
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public abstract class LevelSolverBase
|
||||
public abstract class FullTextLevelSolverBase
|
||||
{
|
||||
public abstract int LevelNumber { get; }
|
||||
private int Year = 2024;
|
||||
@ -24,10 +24,10 @@ public abstract class LevelSolverBase
|
||||
|
||||
public TokenConverter GetData()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
InputSchemaBuilder schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = this.DefineInputSchema(schemaBuilder).Build();
|
||||
|
||||
var parser = new TextParser(schema);
|
||||
var parser = TextParser.Create(schema);
|
||||
var data = parser
|
||||
.SetInputText(this.GetLevelInput())
|
||||
.Parse();
|
@ -5,7 +5,7 @@ using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
|
||||
public class Level1Solver : LevelSolverBase
|
||||
public class Level1Solver : FullTextLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
|
@ -5,7 +5,7 @@ using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
|
||||
public class Level2Solver : LevelSolverBase
|
||||
public class Level2Solver : FullTextLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
|
14
Level3/Level3.csproj
Normal file
14
Level3/Level3.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>
|
80
Level3/Level3Solver.cs
Normal file
80
Level3/Level3Solver.cs
Normal file
@ -0,0 +1,80 @@
|
||||
namespace AoC24;
|
||||
|
||||
using AoC24.Common;
|
||||
using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public class Level3Solver : FragmentLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
get { return 3; }
|
||||
}
|
||||
|
||||
protected override FragmentSchemaBuilder DefineInputSchema(FragmentSchemaBuilder schemaBuilder)
|
||||
{
|
||||
return schemaBuilder
|
||||
.StartOptions()
|
||||
.Option()
|
||||
.Expect("mul(")
|
||||
.Expect(InputType.Integer, "num1")
|
||||
.Expect(",")
|
||||
.Expect(InputType.Integer, "num2")
|
||||
.Expect(")")
|
||||
.Option()
|
||||
.Expect("do()", "on")
|
||||
.Option()
|
||||
.Expect("don't()", "off")
|
||||
.EndOptions();
|
||||
|
||||
}
|
||||
|
||||
public override string SolveFirstStar()
|
||||
{
|
||||
return this.GetData()
|
||||
.AsFragments()
|
||||
.ConvertAll((Fragment f) =>
|
||||
{
|
||||
if(!f.ContainsKey("num1") || f["num1"].Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int numProd = int.Parse(f["num1"][0]);
|
||||
numProd *= int.Parse(f["num2"][0]);
|
||||
return numProd;
|
||||
})
|
||||
.ReduceData((int num1, int num2) => num1 + num2)
|
||||
.ToString();
|
||||
}
|
||||
|
||||
public override string SolveSecondStar()
|
||||
{
|
||||
bool isOn = true;
|
||||
return this.GetData()
|
||||
.AsFragments()
|
||||
.ConvertAll((Fragment f) =>
|
||||
{
|
||||
if(f["on"].Count > 0)
|
||||
{
|
||||
isOn = true;
|
||||
return 0;
|
||||
}
|
||||
if(f["off"].Count > 0)
|
||||
{
|
||||
isOn = false;
|
||||
return 0;
|
||||
}
|
||||
if(!isOn)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int numProd = int.Parse(f["num1"][0]);
|
||||
numProd *= int.Parse(f["num2"][0]) ;
|
||||
return numProd;
|
||||
})
|
||||
.ReduceData((int num1, int num2) => num1 + num2)
|
||||
.ToString();
|
||||
}
|
||||
}
|
24
Level3/Program.cs
Normal file
24
Level3/Program.cs
Normal file
@ -0,0 +1,24 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using AoC24;
|
||||
|
||||
var levelSolver = new Level3Solver();
|
||||
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!");
|
||||
}
|
43
LevelTemplate/LevelXSolver_Fragment.cs
Normal file
43
LevelTemplate/LevelXSolver_Fragment.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace AoC24;
|
||||
|
||||
using AoC24.Common;
|
||||
using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
|
||||
public class LevelXSolver_Fragment : FragmentLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
// TODO: update level number, csproj file and rename class!
|
||||
get { return 1; }
|
||||
}
|
||||
|
||||
protected override FragmentSchemaBuilder DefineInputSchema(FragmentSchemaBuilder schemaBuilder)
|
||||
{
|
||||
return schemaBuilder
|
||||
.Repeat()
|
||||
.Expect(InputType.Integer)
|
||||
.EndRepetition();
|
||||
}
|
||||
|
||||
public override string SolveFirstStar()
|
||||
{
|
||||
var data = this.GetData()
|
||||
.AsFragments();
|
||||
|
||||
// TODO: implement
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public override string SolveSecondStar()
|
||||
{
|
||||
var data = this.GetData()
|
||||
.AsFragments();
|
||||
|
||||
// TODO: implement
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
|
||||
public class LevelXSolver : LevelSolverBase
|
||||
public class LevelXSolver_FullText : FullTextLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
@ -2,9 +2,10 @@
|
||||
|
||||
using AoC24;
|
||||
|
||||
var levelSolver = new LevelXSolver();
|
||||
var solution1 = levelSolver.SolveFirstStar();
|
||||
var solution2 = levelSolver.SolveSecondStar();
|
||||
var levelSolver1 = new LevelXSolver_FullText();
|
||||
var levelSolver2 = new LevelXSolver_Fragment();
|
||||
var solution1 = levelSolver1.SolveFirstStar();
|
||||
var solution2 = levelSolver2.SolveSecondStar();
|
||||
|
||||
if (!string.IsNullOrEmpty(solution1))
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user