Messing with speeding up day 23

This commit is contained in:
2022-12-23 15:27:03 -06:00
parent a121992976
commit 33650996dd

View File

@ -31,9 +31,10 @@ internal class Day23 : Day
} }
} }
private static List<(ivec2 curr, ivec2 proposed)> ConsiderMove(ICollection<ivec2> positions, int orderStart) private static IEnumerable<(ivec2 curr, ivec2 next)> ConsiderMove(ICollection<ivec2> positions, int orderStart)
{ {
List<(ivec2 curr, ivec2 proposed)> considerations = new(positions.Count); Dictionary<ivec2, ivec2> considerations = new(positions.Count);
Dictionary<ivec2, int> dupes = new();
foreach (var elfPos in positions) foreach (var elfPos in positions)
{ {
@ -47,13 +48,26 @@ internal class Day23 : Day
var dirs = dirOrder[(i + orderStart) % dirOrder.Length]; var dirs = dirOrder[(i + orderStart) % dirOrder.Length];
if (!dirs.Any(d => positions.Contains(d + elfPos))) if (!dirs.Any(d => positions.Contains(d + elfPos)))
{ {
considerations.Add((elfPos, elfPos + dirs[0])); var v = elfPos + dirs[0];
if (!dupes.ContainsKey(v))
{
dupes.Add(v, 0);
}
dupes[v]++;
considerations.Add(elfPos, v);
break; break;
} }
} }
} }
return considerations; foreach (var c in considerations)
{
if (dupes[c.Value] == 1)
{
yield return (c.Key, c.Value);
}
}
} }
private static int PlayOut(ICollection<ivec2> positions, int? maxRounds = null) private static int PlayOut(ICollection<ivec2> positions, int? maxRounds = null)
@ -67,30 +81,41 @@ internal class Day23 : Day
break; break;
} }
for (int i = 0; i < considerations.Count; i++) // for (int i = 0; i < considerations.Count; i++)
{ // {
bool bDuped = false; // bool bDuped = false;
for (int j = i + 1; j < considerations.Count; j++) // foreach (var c in considerations.Skip(i + 1).Where(c => c.Value == considerations.ElementAt(i).Value))
{ // {
if (considerations[i].proposed == considerations[j].proposed) // bDuped = true;
{ // considerations.Remove(c.Key);
bDuped = true; // }
considerations.RemoveAt(j); //
j--; // if (bDuped)
} // {
} // considerations.Remove(considerations.ElementAt(i).Key);
// i--;
if (bDuped) // }
{ // // for (int j = i + 1; j < considerations.Count; j++)
considerations.RemoveAt(i); // // {
i--; // // if (considerations[i].proposed == considerations[j].proposed)
} // // {
} // // bDuped = true;
// // considerations.RemoveAt(j);
// // j--;
// // }
// // }
// //
// // if (bDuped)
// // {
// // considerations.RemoveAt(i);
// // i--;
// // }
// }
foreach (var move in considerations) foreach (var move in considerations)
{ {
positions.Remove(move.curr); positions.Remove(move.curr);
positions.Add(move.proposed); positions.Add(move.next);
} }
} }