Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Day11
- {
- public static long Solve1(string input) => return Solve(GetStones(input), 25);
- public static long Solve2(string input) => return Solve(GetStones(input), 75);
- private static long Solve(Dictionary<long, long> Stones, int blinks)
- {
- for (int i = 0; i < blinks; i++)
- {
- if (Stones.ContainsKey(0L))
- {
- Stones.Add(-1, Stones[0L]);
- Stones.Remove(0L);
- }
- foreach (var k in Stones.Keys.Where(kk => kk > 0L).ToArray())
- {
- if (k.ToString().Length % 2 == 0)
- {
- long k1 = -long.Parse(k.ToString().Substring(0, k.ToString().Length / 2));
- long k2 = -long.Parse(k.ToString().Substring(k.ToString().Length / 2, k.ToString().Length / 2));
- Stones.Increase(k1, Stones[k]);
- Stones.Redistribute(k2, k);
- }
- else
- {
- Stones.Redistribute(-k * 2024L, k);
- }
- }
- Stones.Keys.Where(kk => kk < 0L).ToList().ForEach(k => Stones.Redistribute(-k, k));
- }
- return Stones.Values.Sum();
- }
- private static Dictionary<long, long> GetStones(string s)
- {
- var Stones = new Dictionary<long, long>();
- Array.ForEach(s.Split(), stone => Stones.Increase(long.Parse(stone), 1L));
- return Stones;
- }
- private static void Redistribute(this Dictionary<long, long> D, long t, long k)
- {
- D.Increase(t, D[k]);
- D.Remove(k);
- }
- private static void Increase(this Dictionary<long, long> D, long t, long v)
- {
- if (D.ContainsKey(t))
- {
- D[t] += v;
- }
- else
- {
- D.Add(t, v);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment