From 5b9ad5766bd0f782f1a3128e97c257c299755700 Mon Sep 17 00:00:00 2001 From: Simon Diesenreiter Date: Wed, 4 Dec 2024 12:47:10 +0100 Subject: [PATCH] add level 3 and backport textparser changes --- Common/FragmentLevelSolverBase.cs | 37 +++++++++ ...lverBase.cs => FulltextLevelSolverBase.cs} | 6 +- Level1/Level1Solver.cs | 2 +- Level2/Level2Solver.cs | 2 +- Level3/Level3.csproj | 14 ++++ Level3/Level3Solver.cs | 80 +++++++++++++++++++ Level3/Program.cs | 24 ++++++ LevelTemplate/LevelXSolver_Fragment.cs | 43 ++++++++++ ...velXSolver.cs => LevelXSolver_Fulltext.cs} | 2 +- LevelTemplate/Program.cs | 7 +- 10 files changed, 208 insertions(+), 9 deletions(-) create mode 100644 Common/FragmentLevelSolverBase.cs rename Common/{LevelSolverBase.cs => FulltextLevelSolverBase.cs} (84%) create mode 100644 Level3/Level3.csproj create mode 100644 Level3/Level3Solver.cs create mode 100644 Level3/Program.cs create mode 100644 LevelTemplate/LevelXSolver_Fragment.cs rename LevelTemplate/{LevelXSolver.cs => LevelXSolver_Fulltext.cs} (93%) diff --git a/Common/FragmentLevelSolverBase.cs b/Common/FragmentLevelSolverBase.cs new file mode 100644 index 0000000..9aef698 --- /dev/null +++ b/Common/FragmentLevelSolverBase.cs @@ -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; + } +} diff --git a/Common/LevelSolverBase.cs b/Common/FulltextLevelSolverBase.cs similarity index 84% rename from Common/LevelSolverBase.cs rename to Common/FulltextLevelSolverBase.cs index e885654..bb675a2 100644 --- a/Common/LevelSolverBase.cs +++ b/Common/FulltextLevelSolverBase.cs @@ -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(); diff --git a/Level1/Level1Solver.cs b/Level1/Level1Solver.cs index aebee0b..74ae939 100644 --- a/Level1/Level1Solver.cs +++ b/Level1/Level1Solver.cs @@ -5,7 +5,7 @@ using AoCLevelInputProvider; using Parsing; using Parsing.Schema; -public class Level1Solver : LevelSolverBase +public class Level1Solver : FullTextLevelSolverBase { public override int LevelNumber { diff --git a/Level2/Level2Solver.cs b/Level2/Level2Solver.cs index 8231ca2..420b0d1 100644 --- a/Level2/Level2Solver.cs +++ b/Level2/Level2Solver.cs @@ -5,7 +5,7 @@ using AoCLevelInputProvider; using Parsing; using Parsing.Schema; -public class Level2Solver : LevelSolverBase +public class Level2Solver : FullTextLevelSolverBase { public override int LevelNumber { diff --git a/Level3/Level3.csproj b/Level3/Level3.csproj new file mode 100644 index 0000000..34709f5 --- /dev/null +++ b/Level3/Level3.csproj @@ -0,0 +1,14 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + diff --git a/Level3/Level3Solver.cs b/Level3/Level3Solver.cs new file mode 100644 index 0000000..e21d0bd --- /dev/null +++ b/Level3/Level3Solver.cs @@ -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(); + } +} diff --git a/Level3/Program.cs b/Level3/Program.cs new file mode 100644 index 0000000..d6f3cca --- /dev/null +++ b/Level3/Program.cs @@ -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!"); +} diff --git a/LevelTemplate/LevelXSolver_Fragment.cs b/LevelTemplate/LevelXSolver_Fragment.cs new file mode 100644 index 0000000..b66d9cc --- /dev/null +++ b/LevelTemplate/LevelXSolver_Fragment.cs @@ -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; + } +} diff --git a/LevelTemplate/LevelXSolver.cs b/LevelTemplate/LevelXSolver_Fulltext.cs similarity index 93% rename from LevelTemplate/LevelXSolver.cs rename to LevelTemplate/LevelXSolver_Fulltext.cs index 66f96ac..e4679e8 100644 --- a/LevelTemplate/LevelXSolver.cs +++ b/LevelTemplate/LevelXSolver_Fulltext.cs @@ -5,7 +5,7 @@ using AoCLevelInputProvider; using Parsing; using Parsing.Schema; -public class LevelXSolver : LevelSolverBase +public class LevelXSolver_FullText : FullTextLevelSolverBase { public override int LevelNumber { diff --git a/LevelTemplate/Program.cs b/LevelTemplate/Program.cs index 04ff1d6..5480052 100644 --- a/LevelTemplate/Program.cs +++ b/LevelTemplate/Program.cs @@ -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)) {