Skip to content

Commit

Permalink
day 13 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 13, 2023
1 parent b4618fb commit 9afe9f1
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions AdventOfCode/Day13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,34 @@ public Day13()
}

public override ValueTask<string> Solve_1()
{
int sum = Solve(0);
return new(sum.ToString());
}

public override ValueTask<string> Solve_2()
{
int sum = Solve(1);
return new(sum.ToString());
}

private int Solve(int expectedSmudges)
{
int sum = 0;
for (int i = 0; i < _input.Length; ++i)
{
sum += GetMirror(_input[i], Direction.Vertical);
sum += GetMirror(_input[i], Direction.Horizontal) * 100;
sum += GetMirror(_input[i], Direction.Vertical, expectedSmudges);
sum += GetMirror(_input[i], Direction.Horizontal, expectedSmudges) * 100;
}

return new(sum.ToString());
return sum;
}

private int GetMirror(string[] pattern, Direction direction)
private int GetMirror(string[] pattern, Direction direction, int expectedSmudges)
{
int length = direction == Direction.Vertical ? pattern[0].Length : pattern.Length;
for (int i = 0; i < length - 1; ++i)
{
if (IsMirror(pattern, i, direction, length))
if (IsMirror(pattern, i, direction, length, expectedSmudges))
{
return i + 1;
}
Expand All @@ -39,43 +50,51 @@ private int GetMirror(string[] pattern, Direction direction)
return 0;
}

private bool IsMirror(string[] pattern, int splitAfter, Direction direction, int length)
private bool IsMirror(string[] pattern, int splitAfter, Direction direction, int length, int expectedSmudges)
{
int a = splitAfter;
int b = splitAfter + 1;

int smudges = 0;

while (a >= 0 && b < length)
{
if (!(direction == Direction.Vertical ? ColumnsEqual(pattern, a, b) : RowEqual(pattern, a, b)))
smudges += direction == Direction.Vertical ? ColumnsSmudges(pattern, a, b) : RowSmudges(pattern, a, b);
if (smudges > expectedSmudges)
{
return false;
}
a--;
b++;
}
return true;
return smudges == expectedSmudges;
}

private bool ColumnsEqual(string[] pattern, int a, int b)
private int ColumnsSmudges(string[] pattern, int a, int b)
{
int smudges = 0;
for (int i = 0; i < pattern.Length; ++i)
{
if (pattern[i][a] != pattern[i][b])
{
return false;
{
smudges++;
}
}
return true;
return smudges;
}

private bool RowEqual(string[] pattern, int a, int b)
private int RowSmudges(string[] pattern, int a, int b)
{
return pattern[a] == pattern[b];
}
int smudges = 0;
for (int i = 0; i < pattern[0].Length; ++i)
{
if (pattern[a][i] != pattern[b][i])
{
smudges++;
}
}

public override ValueTask<string> Solve_2()
{
return new("TBD");
return smudges;
}

private enum Direction
Expand Down

0 comments on commit 9afe9f1

Please sign in to comment.