From 33650996ddc7d0aaf2ac7226a2913d97df33af8c Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 23 Dec 2022 15:27:03 -0600 Subject: [PATCH] Messing with speeding up day 23 --- src/23.cs | 73 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/src/23.cs b/src/23.cs index 5d23622..434bd4a 100644 --- a/src/23.cs +++ b/src/23.cs @@ -31,9 +31,10 @@ internal class Day23 : Day } } - private static List<(ivec2 curr, ivec2 proposed)> ConsiderMove(ICollection positions, int orderStart) + private static IEnumerable<(ivec2 curr, ivec2 next)> ConsiderMove(ICollection positions, int orderStart) { - List<(ivec2 curr, ivec2 proposed)> considerations = new(positions.Count); + Dictionary considerations = new(positions.Count); + Dictionary dupes = new(); foreach (var elfPos in positions) { @@ -47,13 +48,26 @@ internal class Day23 : Day var dirs = dirOrder[(i + orderStart) % dirOrder.Length]; 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; } } } - return considerations; + foreach (var c in considerations) + { + if (dupes[c.Value] == 1) + { + yield return (c.Key, c.Value); + } + } } private static int PlayOut(ICollection positions, int? maxRounds = null) @@ -67,30 +81,41 @@ internal class Day23 : Day break; } - for (int i = 0; i < considerations.Count; i++) - { - bool bDuped = false; - for (int j = i + 1; j < considerations.Count; j++) - { - if (considerations[i].proposed == considerations[j].proposed) - { - bDuped = true; - considerations.RemoveAt(j); - j--; - } - } - - if (bDuped) - { - considerations.RemoveAt(i); - i--; - } - } + // for (int i = 0; i < considerations.Count; i++) + // { + // bool bDuped = false; + // foreach (var c in considerations.Skip(i + 1).Where(c => c.Value == considerations.ElementAt(i).Value)) + // { + // bDuped = true; + // considerations.Remove(c.Key); + // } + // + // if (bDuped) + // { + // considerations.Remove(considerations.ElementAt(i).Key); + // i--; + // } + // // for (int j = i + 1; j < considerations.Count; j++) + // // { + // // if (considerations[i].proposed == considerations[j].proposed) + // // { + // // bDuped = true; + // // considerations.RemoveAt(j); + // // j--; + // // } + // // } + // // + // // if (bDuped) + // // { + // // considerations.RemoveAt(i); + // // i--; + // // } + // } foreach (var move in considerations) { positions.Remove(move.curr); - positions.Add(move.proposed); + positions.Add(move.next); } }