add a few more solutions
This commit is contained in:
14
Level4/Level4.csproj
Normal file
14
Level4/Level4.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>
|
||||
67
Level4/Level4Solver.cs
Normal file
67
Level4/Level4Solver.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
namespace AoC24;
|
||||
|
||||
using AoC24.Common;
|
||||
using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Data;
|
||||
using Parsing.Schema;
|
||||
|
||||
public class Level4Solver : FullTextLevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
get { return 4; }
|
||||
}
|
||||
|
||||
protected override InputSchemaBuilder DefineInputSchema(InputSchemaBuilder schemaBuilder)
|
||||
{
|
||||
return schemaBuilder
|
||||
.Repeat()
|
||||
.Expect(InputType.Char)
|
||||
.EndRepetition();
|
||||
}
|
||||
|
||||
public override string SolveFirstStar()
|
||||
{
|
||||
var data = this.GetData()
|
||||
.AsListRows<string>();
|
||||
|
||||
var searchSequence = new List<string> { "X", "M", "A", "S" };
|
||||
var manipulator = DefaultTwoDimensionalManipulator.Create(data);
|
||||
var searchResults = manipulator.FindInSet(searchSequence);
|
||||
|
||||
return searchResults.Count.ToString();
|
||||
}
|
||||
|
||||
public override string SolveSecondStar()
|
||||
{
|
||||
var data = this.GetData()
|
||||
.AsListRows<string>();
|
||||
|
||||
var searchSequence = new List<string> { "M", "A", "S" };
|
||||
var manipulator = DefaultTwoDimensionalManipulator.Create(data);
|
||||
var searchResults = manipulator.FindInSet(searchSequence, Direction.NE | Direction.SE | Direction.NW | Direction.SW);
|
||||
|
||||
var indicesComparator = (IDataIndex<int> index1, IDataIndex<int> index2) =>
|
||||
{
|
||||
return index1.GetIndices()[0] == index2.GetIndices()[0] && index1.GetIndices()[1] == index2.GetIndices()[1];
|
||||
};
|
||||
|
||||
var foundMatches = 0;
|
||||
for(int i = 0; i < searchResults.Count; i++)
|
||||
{
|
||||
var secondPositionA = manipulator.Move(searchResults[i].DataIndex, searchResults[i].Direction);
|
||||
for(int j = i+1; j < searchResults.Count; j++)
|
||||
{
|
||||
var secondPositionB = manipulator.Move(searchResults[j].DataIndex, searchResults[j].Direction);
|
||||
if(indicesComparator(secondPositionA, secondPositionB))
|
||||
{
|
||||
foundMatches++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return foundMatches.ToString();
|
||||
}
|
||||
}
|
||||
24
Level4/Program.cs
Normal file
24
Level4/Program.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using AoC24;
|
||||
|
||||
var levelSolver = new Level4Solver();
|
||||
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