Skip to content

Commit

Permalink
day(22): Tidied up using optimisations from the sub
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 22, 2024
1 parent 6aecdaf commit d90c44c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
61 changes: 22 additions & 39 deletions src/AdventOfCode/Day22.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AdventOfCode.Utilities;

namespace AdventOfCode
{
Expand All @@ -16,44 +17,43 @@ public long Part1(string[] input)

public long Part2(string[] input)
{
long[] seeds = input.Select(long.Parse).ToArray();
var results = new long[1 << 20];
var seen = new int[1 << 20];

(int Price, int Diff)[][] prices = seeds.Select(s => PriceSequence(s).ToArray()).ToArray();
int[] prices = new int[2001];
int[] diffs = new int[2001];

var results = new Dictionary<(int, int, int, int), long>();
int[] sequence = new int[4];

for (int i = 0; i < seeds.Length; i++)
foreach ((int id, long seed) in input.Select(long.Parse).Enumerate(start: 1))
{
var seen = new HashSet<(int, int, int, int)>();
(int Price, int Diff)[] diffs = prices[i];
prices[0] = (int)seed % 10;

for (int j = 3; j < diffs.Length; j++)
foreach ((int i, long n) in NumberSequence(seed).Enumerate(start: 1))
{
sequence[0] = diffs[j - 3].Diff;
sequence[1] = diffs[j - 2].Diff;
sequence[2] = diffs[j - 1].Diff;
sequence[3] = diffs[j].Diff;
prices[i] = (int)n % 10;
diffs[i] = prices[i] - prices[i - 1];
}

var key = (sequence[0], sequence[1], sequence[2], sequence[3]);
for (int i = 4; i < diffs.Length; i++)
{
// store the 4 elements of the sequence as a 20bit number, 5 bits per value
int sequence = ((diffs[i - 3] + 9) << 15)
| ((diffs[i - 2] + 9) << 10)
| ((diffs[i - 1] + 9) << 5)
| (diffs[i] + 9);

if (!seen.Contains(key)) // make sure we only take the first instance of the sequence
if (seen[sequence] != id) // make sure we only take the first instance of the sequence
{
results[key] = results.GetValueOrDefault(key) + diffs[j].Price;
seen.Add(key);
results[sequence] += prices[i];
seen[sequence] = id;
}
}
}

//var foo = results[(-2, 1, -1, 3)];

return results.Values.Max();
return results.Max();
}

private static IEnumerable<long> NumberSequence(long seed)
{
yield return seed;

for (int i = 0; i < 2000; i++)
{
seed ^= (seed * 64);
Expand All @@ -66,22 +66,5 @@ private static IEnumerable<long> NumberSequence(long seed)
yield return seed;
}
}

private static IEnumerable<(int Price, int Diff)> PriceSequence(long seed)
{
// why did I not just do this with zip...?

using IEnumerator<long> cursor = NumberSequence(seed).GetEnumerator();

cursor.MoveNext();
int previous = (int)(cursor.Current % 10);

while (cursor.MoveNext())
{
int price = (int)(cursor.Current % 10);
yield return (price, price - previous);
previous = price;
}
}
}
}
4 changes: 2 additions & 2 deletions src/AdventOfCode/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> @t
return value;
}

public static IEnumerable<(int i, T item)> Enumerate<T>(this IEnumerable<T> @this)
public static IEnumerable<(int i, T item)> Enumerate<T>(this IEnumerable<T> @this, int start = 0)
{
int i = 0;
int i = start;

foreach (T item in @this)
{
Expand Down

0 comments on commit d90c44c

Please sign in to comment.