Skip to content

Commit

Permalink
day 11 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 11, 2023
1 parent 7aef494 commit 6509ff2
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions AdventOfCode/Day11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ public Day11()
}

public override ValueTask<string> Solve_1()
{
return new(Solve(2).ToString());
}

public override ValueTask<string> Solve_2()
{
return new(Solve(1000000).ToString());
}

private long Solve(int factor)
{
List<Galaxy> galaxies = new List<Galaxy>();

Expand All @@ -24,35 +34,29 @@ public override ValueTask<string> Solve_1()
}
}

List<Galaxy> expanded = Expand(galaxies);
List<Galaxy> expanded = Expand(galaxies, factor);

int sum = 0;
long sum = 0;
for (int i = 0; i < expanded.Count - 1; ++i)
{
for (int j = i + 1; j < expanded.Count; ++j)
{
int path = GetPath(expanded[i], expanded[j]);
long path = GetPath(expanded[i], expanded[j]);
sum += path;
}
}

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

private int GetPath(Galaxy galaxy1, Galaxy galaxy2)
private long GetPath(Galaxy galaxy1, Galaxy galaxy2)
{
int iDiff = Math.Abs(galaxy1.I - galaxy2.I);
int jDiff = Math.Abs(galaxy1.J - galaxy2.J);
int path = iDiff + jDiff;
long iDiff = Math.Abs(galaxy1.I - galaxy2.I);
long jDiff = Math.Abs(galaxy1.J - galaxy2.J);
long path = iDiff + jDiff;
return path;
}

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

private List<Galaxy> Expand(List<Galaxy> galaxies)
private List<Galaxy> Expand(List<Galaxy> galaxies, int factor)
{
List<int> iToExpand = new List<int>();
List<int> jToExpand = new List<int>();
Expand All @@ -74,12 +78,12 @@ private List<Galaxy> Expand(List<Galaxy> galaxies)
List<Galaxy> expanded = new List<Galaxy>();
for (int i = 0; i < galaxies.Count; ++i)
{
int newI = galaxies[i].I + iToExpand.Count(c => c < galaxies[i].I);
int newJ = galaxies[i].J + jToExpand.Count(c => c < galaxies[i].J);
long newI = galaxies[i].I + iToExpand.Count(c => c < galaxies[i].I) * (factor - 1);
long newJ = galaxies[i].J + jToExpand.Count(c => c < galaxies[i].J) * (factor - 1);
expanded.Add(new Galaxy(newI, newJ));
}
return expanded;
}

private record Galaxy(int I, int J);
private record Galaxy(long I, long J);
}

0 comments on commit 6509ff2

Please sign in to comment.