Skip to content

Commit

Permalink
2024 Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Dec 4, 2024
1 parent 23b2c54 commit 968e8d0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
57 changes: 54 additions & 3 deletions src/2024/04/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;

var lines = Resources.GetInputFileLines("input.txt");
var map = Resources.GetInputFileLines().ParseAsArray();

Console.WriteLine($"Part 1: {""}");
Console.WriteLine($"Part 2: {""}");
// Take all possible vectors across the map (rows, cols, diagonals)
int part1 = map.GetVectors()
.SelectMany(vector => Enumerable.Range(0, vector.Count - 3).Select(i => vector.Skip(i).Take(4).ToList()))
.Count(vector => vector.VectorContainsWord(map, "XMAS") || vector.VectorContainsWord(map, "SAMX"));

// Get all X constelations of coordinates across the map
int part2 = map.GetXMasks()
.Count(MaskContainsMasMas);

Console.WriteLine($"Part 1: {part1}");
Console.WriteLine($"Part 2: {part2}");

bool MaskContainsMasMas(IList<Coor> mask)
{
if (map.Get(mask[2]) != 'A') return false;

var lt = map.Get(mask[0]);
var rt = map.Get(mask[1]);
var lb = map.Get(mask[3]);
var rb = map.Get(mask[4]);

return ((lt == 'M' && rb == 'S') || (lt == 'S' && rb == 'M'))
&& ((rt == 'M' && lb == 'S') || (rt == 'S' && lb == 'M'));
}

static file class Extensions
{
public static bool VectorContainsWord(this IList<Coor> vector, char[,] map, string word)
{
if (vector.Count < word.Length) return false;
if (vector.Where((c, i) => map.Get(vector[i]) != word[i]).Any()) return false;
return true;
}

// Returns sets of coordinates in the shape of an X
public static IEnumerable<List<Coor<int>>> GetXMasks(this char[,] map)
{
var width = map.Width();
var height = map.Height();

for (int row = 0; row < height - 2; ++row)
for (int col = 0; col < width - 2; ++col)
yield return
[
new Coor<int>(row, col),
new Coor<int>(row, col + 2),
new Coor<int>(row + 1, col + 1),
new Coor<int>(row + 2, col),
new Coor<int>(row + 2, col + 2),
];
}
}
47 changes: 46 additions & 1 deletion src/Common/MultiDimensionalArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text;
using System.Drawing;
using System.Linq;
using System.Text;

namespace AdventOfCode.Common;

Expand Down Expand Up @@ -88,6 +90,49 @@ public static void Print<T>(this T[,] map, Func<Coor<int>, char> printOverride)
}
}

// Returns a set of vectors of possible ways to traverse a rectangle:
// - Rows
// - Columns
// - Diagonals
public static IEnumerable<List<Coor<int>>> GetVectors<T>(this T[,] map)
{
var width = map.Width();
var height = map.Height();

// Horizontal rows
for (int row = 0; row < width; row++)
yield return [.. Enumerable.Range(0, width).Select(col => new Coor<int>(row, col))];

// Vertical cols
for (int col = 0; col < width; col++)
yield return [.. Enumerable.Range(0, height).Select(row => new Coor<int>(row, col))];

for (int i = 0; i < height + width - 1; i++)
{
var diagonal1 = new List<Coor<int>>();
var diagonal2 = new List<Coor<int>>();

int row = Math.Min(i, height - 1);
int col = Math.Max(0, i - height + 1);
var c1 = new Coor<int>(row, col);
var c2 = new Coor<int>(row, width - col - 1);

do
{
diagonal1.Add(c1);
diagonal2.Add(c2);
row--;
col++;
c1 = new Coor<int>(row, col);
c2 = new Coor<int>(row, width - col - 1);
}
while (c1.InBoundsOf(map));

yield return diagonal1;
yield return diagonal2;
}
}

public static T Get<T>(this T[,] items, Coor<int> coor)
=> items[coor.Y, coor.X];

Expand Down

0 comments on commit 968e8d0

Please sign in to comment.