Skip to content

Commit

Permalink
day 8 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 8, 2024
1 parent 9a9376c commit e01f596
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions AdventOfCode/Day08.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ public override ValueTask<string> Solve_1()
HashSet<Point> antinodes = new HashSet<Point>();
foreach (var kv in antennas)
{
antinodes.UnionWith(GetAntinodes(kv.Value));
antinodes.UnionWith(GetAntinodes(kv.Value, updated: false));
}

return new(antinodes.Count.ToString());
}

public override ValueTask<string> Solve_2()
{
return new("");
Dictionary<char, List<Point>> antennas = GetAntennas();

HashSet<Point> antinodes = new HashSet<Point>();
foreach (var kv in antennas)
{
antinodes.UnionWith(GetAntinodes(kv.Value, updated: true));
}

return new(antinodes.Count.ToString());
}

private Dictionary<char, List<Point>> GetAntennas()
Expand All @@ -47,37 +55,64 @@ private Dictionary<char, List<Point>> GetAntennas()
return antennas;
}

private HashSet<Point> GetAntinodes(List<Point> antennas)
private HashSet<Point> GetAntinodes(List<Point> antennas, bool updated)
{
HashSet<Point> antinodes = new HashSet<Point>();

for (int i = 0; i < antennas.Count - 1; ++i)
{
for (int j = i + 1; j < antennas.Count; ++j)
{
(Point? antinodeOne, Point? antinodeTwo) = GetAntinodes(antennas[i], antennas[j]);

if (antinodeOne != null)
{
antinodes.Add(antinodeOne);
}
if (antinodeTwo != null)
{
antinodes.Add(antinodeTwo);
}
List<Point> a = updated
? GetAntinodesUpdated(antennas[i], antennas[j]) : GetAntinodes(antennas[i], antennas[j]);
antinodes.UnionWith(a);
}
}

return antinodes;
}

private (Point? antinodeOne, Point? antinodeTwo) GetAntinodes(Point a, Point b)
private List<Point> GetAntinodes(Point a, Point b)
{
List<Point> result = new List<Point>();

var antinodeOne = new Point(2 * b.I - a.I, 2 * b.J - a.J);
var antinodeTwo = new Point(2 * a.I - b.I, 2 * a.J - b.J);

return (IsWithinBounds(antinodeOne) ? antinodeOne : null,
IsWithinBounds(antinodeTwo) ? antinodeTwo : null);
if (IsWithinBounds(antinodeOne))
{
result.Add(antinodeOne);
}
if (IsWithinBounds(antinodeTwo))
{
result.Add(antinodeTwo);
}

return result;
}

private List<Point> GetAntinodesUpdated(Point a, Point b)
{
List<Point> resultOne = GetAntinodesUpdatedOneDirection(a, b);
List<Point> resultTwo = GetAntinodesUpdatedOneDirection(b, a);

return resultOne.Concat(resultTwo).ToList();
}

private List<Point> GetAntinodesUpdatedOneDirection(Point a, Point b)
{
List<Point> result = new List<Point>();

int n = 1;
var antinode = new Point(n * b.I - (n - 1) * a.I, n * b.J - (n - 1) * a.J);
while (IsWithinBounds(antinode))
{
result.Add(antinode);
n++;
antinode = new Point(n * b.I - (n - 1) * a.I, n * b.J - (n - 1) * a.J);
}

return result;
}

private bool IsWithinBounds(Point point)
Expand Down

0 comments on commit e01f596

Please sign in to comment.