Skip to content

Commit

Permalink
2023 Day 13 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Dec 13, 2023
1 parent b063020 commit e2c2817
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/2023/13/Program.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
using AdventOfCode.Common;
using GetVectors = System.Func<int, int, int, (System.Collections.Generic.IEnumerable<bool>, System.Collections.Generic.IEnumerable<bool>)>;

var patterns = Resources.GetInputFileContent()
.SplitBy(Environment.NewLine + Environment.NewLine)
.Select(pattern => pattern.SplitBy(Environment.NewLine))
.Select(pattern => pattern.ParseAsArray(c => c == '#'))
.ToList();

var result = 0;
foreach (var pattern in patterns)
{
var height = pattern.GetLength(0);
var width = pattern.GetLength(1);

var reflectedRow = GetReflection(width, height, (int row, int col, int size) =>
(Enumerable.Range(0, size).Select(i => pattern[row + i, col]).BitsToInt(),
Enumerable.Range(1, size).Select(i => pattern[row - i, col]).BitsToInt()));
Console.WriteLine($"Part 1: {FindReflections(patterns, numOfSmudges: 0)}");
Console.WriteLine($"Part 2: {FindReflections(patterns, numOfSmudges: 1)}");

if (reflectedRow.HasValue)
{
result += 100 * reflectedRow.Value;
}
else
static int FindReflections(List<bool[,]> patterns, int numOfSmudges)
{
var result = 0;
foreach (var pattern in patterns)
{
var reflectedCol = GetReflection(height, width, (int col, int row, int size) =>
(Enumerable.Range(0, size).Select(i => pattern[row, col + i]).BitsToInt(),
Enumerable.Range(1, size).Select(i => pattern[row, col - i]).BitsToInt()));
var height = pattern.GetLength(0);
var width = pattern.GetLength(1);

if (reflectedCol.HasValue)
var reflectedRow = GetReflectionPoint(width, height, numOfSmudges, (int row, int col, int size) =>
(Enumerable.Range(0, size).Select(i => pattern[row + i, col]),
Enumerable.Range(1, size).Select(i => pattern[row - i, col])));

if (reflectedRow.HasValue)
{
result += reflectedCol.Value;
result += 100 * reflectedRow.Value;
}
else
{
throw new Exception("No reflection found for pattern");
var reflectedCol = GetReflectionPoint(height, width, numOfSmudges, (int col, int row, int size) =>
(Enumerable.Range(0, size).Select(i => pattern[row, col + i]),
Enumerable.Range(1, size).Select(i => pattern[row, col - i])));

result += reflectedCol ?? throw new Exception("No reflection found for pattern");
}
}
}

Console.WriteLine($"Part 1: {result}");
return result;
}

static int? GetReflection(int width, int height, Func<int, int, int, (int, int)> getReflections)
static int? GetReflectionPoint(int width, int height, int numOfSmudges, GetVectors getReflections)
{
for (int dim1 = 1; dim1 < height; dim1++)
for (int row = 1; row < height; row++)
{
var isReflection = Enumerable.Range(0, width)
.All(dim2 =>
var differences = Enumerable.Range(0, width)
.Sum(col =>
{
var size = Math.Min(dim1, height - dim1);
var (a, b) = getReflections(dim1, dim2, size);
return a == b;
var size = Math.Min(row, height - row);
var (a, b) = getReflections(row, col, size);
return GetDifferences(a, b);
});

if (isReflection)
if (differences == numOfSmudges)
{
return dim1;
return row;
}
}

return null;
}

static int GetDifferences(IEnumerable<bool> a, IEnumerable<bool> b)
=> a.Zip(b).Count(p => p.First != p.Second);

0 comments on commit e2c2817

Please sign in to comment.