Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var timer = System.Diagnostics.Stopwatch.StartNew();
- var stones = (await File.ReadAllTextAsync("input.txt")).Split(' ').ToDictionary(long.Parse, _ => 1L);
- Console.WriteLine($"Parsing complete. ({timer.ElapsedMilliseconds}ms)");
- timer.Restart();
- Console.WriteLine($"Part 1: {CountStones(25, stones)} ({timer.ElapsedMilliseconds}ms)");
- timer.Restart();
- Console.WriteLine($"Part 2: {CountStones(75, stones)} ({timer.ElapsedMilliseconds}ms)");
- return;
- static long CountStones(int iterations, Dictionary<long, long> stones)
- {
- for (var i = 0; i < iterations; i++)
- {
- var newStones = new Dictionary<long, long>();
- foreach (var (stone, count) in stones)
- {
- var length = Math.Floor(Math.Log10(stone) + 1);
- if (stone == 0)
- {
- AddStones(1, count);
- }
- else if (length % 2 == 0)
- {
- var left = (long)(stone / Math.Pow(10, length / 2));
- var right = (long)(stone % Math.Pow(10, length / 2));
- AddStones(left, count);
- AddStones(right, count);
- }
- else
- {
- AddStones(stone * 2024, count);
- }
- }
- stones = newStones;
- continue;
- void AddStones(long stone, long count)
- {
- newStones.TryAdd(stone, 0);
- newStones[stone] += count;
- }
- }
- return stones.Values.Sum();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement