Implemented Day03 part1 and part2 for 2026
This commit is contained in:
1 parent
b94ab07924
commit
3510234ed8
3 files changed
+179
-4
No files matched your search
+2
-4
@@ -33,10 +33,8 @@ public class Day02 : IPuzzleSolver<long>
|
||||
return program
|
||||
.Prepend(waypoint)
|
||||
.SlidingWindow(2)
|
||||
.Aggregate(0, (acc, next) =>
|
||||
next.Length != 2
|
||||
? acc
|
||||
: acc + GetDistance(next[0], next[1]));
|
||||
.Skip(1)
|
||||
.Aggregate(0, (acc, next) => acc + GetDistance(next[0], next[1]));
|
||||
}
|
||||
|
||||
private static int GetDistance(char current, char next)
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
using AdvenOfCode.Contracts;
|
||||
using AdventOfCode.Extensions;
|
||||
|
||||
namespace AoC_2026;
|
||||
|
||||
public class Day03 : IPuzzleSolver<long>
|
||||
{
|
||||
private char[][] ParsePuzzleInput(string path)
|
||||
{
|
||||
var puzzleInput = File.ReadAllLines(path)
|
||||
.Where(l => !string.IsNullOrWhiteSpace(l))
|
||||
.Select(l => l.Select(c => c).ToArray())
|
||||
.ToArray();
|
||||
return puzzleInput;
|
||||
}
|
||||
|
||||
public long SolvePart1(string pathToPuzzleInput)
|
||||
{
|
||||
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
||||
var numberOfSquares = GetNumberOfSquares(forestGrid);
|
||||
return numberOfSquares;
|
||||
}
|
||||
|
||||
private static int GetNumberOfSquares(char[][] forestGrid)
|
||||
{
|
||||
return forestGrid
|
||||
.Select((gridRow, i) =>
|
||||
GetTopCorners(gridRow)
|
||||
.Combinations(2)
|
||||
.Select(pair => (topCorners: pair, bottomIndex: GetBottomEdgeIndex(i, pair)))
|
||||
.Where(tuple => IsBottomEdgeInBounds(tuple.bottomIndex, forestGrid.Length))
|
||||
.Count(tuple => IsValidSquare(forestGrid[tuple.bottomIndex], tuple.topCorners)))
|
||||
.Sum();
|
||||
}
|
||||
|
||||
public long SolvePart2(string pathToPuzzleInput)
|
||||
{
|
||||
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
||||
var numberOfSquares = GetNumberOfTwistedSquares(forestGrid);
|
||||
return numberOfSquares;
|
||||
}
|
||||
|
||||
private int GetNumberOfTwistedSquares(char[][] forestGrid)
|
||||
{
|
||||
var coords = GridToCoordinates(forestGrid);
|
||||
return coords
|
||||
.Where(coord => IsTreeAtPoint(coord, forestGrid))
|
||||
.Sum(coord => GetAllPossibleCorners(forestGrid, coord.x, coord.y)
|
||||
.Where(c => AllPointsAreInBounds(c, forestGrid.Length, forestGrid[coord.x].Length))
|
||||
.Where(c => AllPointsAreSameHeightOrBelow(c, coord.x))
|
||||
.Where(c => OnePointIsToTheRight(c, coord.y))
|
||||
.Count(c => AllPointsAreTrees(forestGrid, c)));
|
||||
}
|
||||
|
||||
private static IEnumerable<(int x, int y)> GridToCoordinates(char[][] forestGrid)
|
||||
{
|
||||
return Enumerable
|
||||
.Range(0, forestGrid.Length)
|
||||
.SelectMany(i => Enumerable
|
||||
.Range(0, forestGrid[i].Length)
|
||||
.Select(j => (x: i, y: j)));
|
||||
}
|
||||
|
||||
private static bool OnePointIsToTheRight((int x, int y)[] corners, int currentRight)
|
||||
{
|
||||
return corners.Any(c => c.y > currentRight);
|
||||
}
|
||||
|
||||
private static bool AllPointsAreSameHeightOrBelow((int x, int y)[] corners, int currentHeight)
|
||||
{
|
||||
return corners.All(c => c.x >= currentHeight);
|
||||
}
|
||||
|
||||
private static bool AllPointsAreTrees(char[][] forestGrid, (int x, int y)[] corners)
|
||||
{
|
||||
return corners.All(c => IsTreeAtPoint(c, forestGrid));
|
||||
}
|
||||
|
||||
private static bool IsTreeAtPoint((int x, int y) point, char[][] forestGrid)
|
||||
{
|
||||
return forestGrid[point.x][point.y] == '#';
|
||||
}
|
||||
|
||||
private static bool AllPointsAreInBounds((int x, int y)[] points, int length, int width)
|
||||
{
|
||||
return points.All(p => p.x >= 0 && p.x < length && p.y >= 0 && p.y < width);
|
||||
}
|
||||
|
||||
private static List<(int x, int y)[]> GetAllPossibleCorners(char[][] forestGrid, int x, int y)
|
||||
{
|
||||
var allCorners = new List<(int x, int y)[]>();
|
||||
var length = forestGrid[x].Length - 1;
|
||||
for (var j = 0; j < length; j++)
|
||||
{
|
||||
var squareWidth = length - j;
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
var corners = new (int x, int y)[4];
|
||||
corners[0] = (x, y);
|
||||
corners[1] = (x + i, y + (squareWidth - i));
|
||||
corners[2] = (x + i + (squareWidth - i), y + (squareWidth - i) - i);
|
||||
corners[3] = (x + (squareWidth - i), y - i);
|
||||
allCorners.Add(corners);
|
||||
}
|
||||
}
|
||||
|
||||
return allCorners;
|
||||
}
|
||||
|
||||
|
||||
private static int GetBottomEdgeIndex(int startRow, int[] pair)
|
||||
{
|
||||
return startRow + pair[1] - pair[0];
|
||||
}
|
||||
|
||||
private static bool IsBottomEdgeInBounds(int index, int forestGridLength)
|
||||
{
|
||||
return index < forestGridLength;
|
||||
}
|
||||
|
||||
private static int[] GetTopCorners(char[] row)
|
||||
{
|
||||
return [.. row.Index().Where(elem => elem.Item == '#').Select(elem => elem.Index)];
|
||||
}
|
||||
|
||||
private static bool IsValidSquare(char[] forestGridRow, int[] cornerPair)
|
||||
{
|
||||
return forestGridRow[cornerPair[0]] == '#' && forestGridRow[cornerPair[1]] == '#';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user