Slightly faster day 15, still not great

This commit is contained in:
2020-12-15 09:02:28 -06:00
parent da7886e2ad
commit 1c32616861

20
Q15.cs
View File

@ -34,15 +34,15 @@ namespace _2020
static int GetNumAt(int turn) static int GetNumAt(int turn)
{ {
var numsSaidDict = new Dictionary<int, List<int>>(); var numsSaidDict = new Dictionary<int, int[]>();
int lastNumSaid = 0; int lastNumSaid = 0;
List<int> entry = null; int[] entry = null;
for (int i = 0; i < turn; i++) for (int i = 0; i < turn; i++)
{ {
if (i < list.Count) if (i < list.Count)
{ {
numsSaidDict.Add(list[i], new List<int>() { i }); numsSaidDict.Add(list[i], new int[2] { i, -1 });
lastNumSaid = list[i]; lastNumSaid = list[i];
} }
else else
@ -55,7 +55,7 @@ namespace _2020
{ {
if (entry != null) if (entry != null)
{ {
lastNumSaid = entry.Count == 1 ? 0 : entry[^1] - entry[^2]; lastNumSaid = entry[1] == -1 ? 0 : entry[0] - entry[1];
} }
else else
{ {
@ -66,11 +66,19 @@ namespace _2020
numsSaidDict.TryGetValue(lastNumSaid, out entry); numsSaidDict.TryGetValue(lastNumSaid, out entry);
if (entry == null) if (entry == null)
{ {
entry = new List<int>(); entry = new int[2] { -1, -1 };
numsSaidDict.Add(lastNumSaid, entry); numsSaidDict.Add(lastNumSaid, entry);
} }
entry.Add(i); if (entry[0] == -1)
{
entry[0] = i;
}
else
{
entry[1] = entry[0];
entry[0] = i;
}
} }
} }