Improve day 14 part 2 runtime

I realized I was looking for the third occurrence when the second works just as well.
This commit is contained in:
2023-12-14 23:05:46 -06:00
parent 91fcf39e17
commit 42e1a27806

View File

@ -164,10 +164,10 @@ internal class Day14 : Day
Tilt(part2, eastDir); Tilt(part2, eastDir);
var result = Stringify(part2); var result = Stringify(part2);
if (seen.Count(g => g == result) == 2) if (seen.Any(g => g == result))
{ {
int periodStart = seen.FindIndex(g => g == result); int periodStart = seen.FindIndex(g => g == result);
int periodLen = seen.Skip(periodStart + 1).ToList().FindIndex(g => g == result) + 1; int periodLen = seen.Count - periodStart;
loopCycle = ((1000000000L - periodStart) % periodLen) + periodStart; loopCycle = ((1000000000L - periodStart) % periodLen) + periodStart;
break; break;
} }