diff options
author | Ben Harris <ben@tilde.team> | 2019-12-12 16:27:25 -0500 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2019-12-12 16:29:53 -0500 |
commit | 16a5eff3bd5961ce5e4ac7aca74de7ed978f84f4 (patch) | |
tree | 5d55cfa23ed43017acb5ae1a9ed8bcb7ce72e26f /Day12.cs | |
parent | a0b13e3276a44e722a9c16ef018f04e05fe2e6b4 (diff) |
optimize day 12
Diffstat (limited to 'Day12.cs')
-rw-r--r-- | Day12.cs | 37 |
1 files changed, 7 insertions, 30 deletions
diff --git a/Day12.cs b/Day12.cs index 43682c1..40d7b1e 100644 --- a/Day12.cs +++ b/Day12.cs @@ -109,40 +109,17 @@ namespace aoc2019 public override string Part2() { - moons = startingPositions; - step = 0; - var seenX = new HashSet<string>(); - var seenY = new HashSet<string>(); - var seenZ = new HashSet<string>(); - int repX = 0, repY = 0, repZ = 0; - - while (true) - { - if (repX != 0 && repY != 0 && repZ != 0) break; - Step(); + int cycleX = 0, cycleY = 0, cycleZ = 0; - if (repX == 0) - { - var xcoords = moons.Select(m => (m.x, m.dx)).ToDelimitedString(); - if (seenX.Contains(xcoords)) repX = step; - seenX.Add(xcoords); - } - if (repY == 0) - { - var ycoords = moons.Select(m => (m.y, m.dy)).ToDelimitedString(); - if (seenY.Contains(ycoords)) repY = step; - seenY.Add(ycoords); - } - if (repZ == 0) - { - var zcoords = moons.Select(m => (m.z, m.dz)).ToDelimitedString(); - if (seenZ.Contains(zcoords)) repZ = step; - seenZ.Add(zcoords); - } + while (cycleX == 0 || cycleY == 0 || cycleZ == 0) { + Step(); step++; + if (cycleX == 0 && moons.All(m => m.dx == 0)) cycleX = step * 2; + if (cycleY == 0 && moons.All(m => m.dy == 0)) cycleY = step * 2; + if (cycleZ == 0 && moons.All(m => m.dz == 0)) cycleZ = step * 2; } - return $"{LCM(repX, LCM(repY, repZ))}"; + return $"{LCM(cycleX, LCM(cycleY, cycleZ))}"; } } } |