Skip to content

Commit

Permalink
Compute depths
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Dec 19, 2024
1 parent 4ce45ed commit d6125cb
Showing 1 changed file with 21 additions and 89 deletions.
110 changes: 21 additions & 89 deletions src/2024/11/Program.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
using System.Collections.Concurrent;
using AdventOfCode.Common;
using AdventOfCode.Common;

var stones = Resources.GetInputFileLines()
.First()
.SplitToNumbers(" ")
.Select(v => new Stone((ulong)v, 0));

Console.WriteLine(string.Join(", ", stones.Select(s => ReturnDifferentStones(s.Number).Count)));
Console.WriteLine(string.Join(", ", stones.Select(s => ReturnDifferentStones(s).Count)));

static HashSet<ulong> ReturnDifferentStones(ulong number)
static Dictionary<ulong, int> ReturnDifferentStones(Stone startingStone)
{
HashSet<ulong> differentStones = [ number ];
var queue = new ConcurrentQueue<ulong>();
queue.Enqueue(number);
var previousCount = 0;
Dictionary<ulong, int> differentStones = new()
{
[startingStone.Number] = startingStone.Generation
};
var queue = new Queue<Stone>();
queue.Enqueue(startingStone);

while (!queue.IsEmpty && previousCount != differentStones.Count)
while (queue.Count > 0)
{
previousCount = differentStones.Count;
if (!queue.TryDequeue(out Stone n)) continue;

if (!queue.TryDequeue(out ulong n)) continue;
var (first, second) = Blink(n.Number);

var (first, second) = Blink(n);
void TryEnqueue(Stone s)
{
if (differentStones.TryAdd(s.Number, s.Generation))
{
queue.Enqueue(s);
}
}

queue.Enqueue(first);
differentStones.Add(first);
TryEnqueue(new Stone(first, n.Generation + 1));

if (second.HasValue)
{
queue.Enqueue(second.Value);
differentStones.Add(second.Value);
TryEnqueue(new Stone(second.Value, n.Generation + 1));
}
}

Expand All @@ -56,77 +61,4 @@ static HashSet<ulong> ReturnDifferentStones(ulong number)
return (left, right);
}

/*var queue = new ConcurrentQueue<Stone>();
var maxGenerations = 75;
var total = 0UL;
var differentStones = new ConcurrentDictionary<ulong, bool>();
foreach (var s in stones)
{
Console.WriteLine("Starting stone " + s.Number);
queue.Enqueue(s);
while (!queue.IsEmpty)
{
Console.Clear();
Console.Write("[");
var gen = queue.First().Generation;
Console.Write(new string('|', gen));
Console.Write(new string(' ', maxGenerations - gen));
Console.WriteLine($"] {gen} / {maxGenerations} ({queue.Count})");
Console.WriteLine("Total: " + total);
Console.WriteLine("Different ones: " + differentStones.Keys.Count);
//Console.WriteLine(string.Join(", ", queue.Select(s => $"{s.Number},{s.Generation}")));
Parallel.ForEach(queue, _ =>
{
if (!queue.TryDequeue(out Stone stone)) return;
var newGeneration = stone.Generation + 1;
if (stone.Number == 0)
{
Enqueue(new Stone(1, newGeneration));
return;
}
var digits = (int)(Math.Log10(stone.Number) + 1);
if (digits % 2 != 0)
{
Enqueue(new Stone(stone.Number * 2024, newGeneration));
return;
}
var div = (ulong)Math.Pow(10, digits / 2);
var left = stone.Number / div;
var right = stone.Number - left * div;
Enqueue(new Stone(left, newGeneration));
Enqueue(new Stone(right, newGeneration));
});
}
}
Console.WriteLine(total);
void Enqueue(Stone stone)
{
if (stone.Generation == maxGenerations)
{
lock (stones)
{
//Console.WriteLine(stone.Number);
Interlocked.Increment(ref total);
}
}
else
{
queue.Enqueue(stone);
differentStones.TryAdd(stone.Number, true);
}
}
Console.WriteLine($"Part 1: {total}");
Console.WriteLine($"Part 2: {""}");*/

readonly file record struct Stone(ulong Number, int Generation);

0 comments on commit d6125cb

Please sign in to comment.