Guest User

2024day11everyblink

a guest
Dec 11th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | Source Code | 0 0
  1. public static class Day11
  2. {
  3. public static long Solve1(string input) => return Solve(GetStones(input), 25);
  4.  
  5. public static long Solve2(string input) => return Solve(GetStones(input), 75);
  6.  
  7. private static long Solve(Dictionary<long, long> Stones, int blinks)
  8. {
  9. for (int i = 0; i < blinks; i++)
  10. {
  11. if (Stones.ContainsKey(0L))
  12. {
  13. Stones.Add(-1, Stones[0L]);
  14. Stones.Remove(0L);
  15. }
  16. foreach (var k in Stones.Keys.Where(kk => kk > 0L).ToArray())
  17. {
  18. if (k.ToString().Length % 2 == 0)
  19. {
  20. long k1 = -long.Parse(k.ToString().Substring(0, k.ToString().Length / 2));
  21. long k2 = -long.Parse(k.ToString().Substring(k.ToString().Length / 2, k.ToString().Length / 2));
  22. Stones.Increase(k1, Stones[k]);
  23. Stones.Redistribute(k2, k);
  24. }
  25. else
  26. {
  27. Stones.Redistribute(-k * 2024L, k);
  28. }
  29. }
  30. Stones.Keys.Where(kk => kk < 0L).ToList().ForEach(k => Stones.Redistribute(-k, k));
  31. }
  32. return Stones.Values.Sum();
  33. }
  34. private static Dictionary<long, long> GetStones(string s)
  35. {
  36. var Stones = new Dictionary<long, long>();
  37. Array.ForEach(s.Split(), stone => Stones.Increase(long.Parse(stone), 1L));
  38. return Stones;
  39. }
  40. private static void Redistribute(this Dictionary<long, long> D, long t, long k)
  41. {
  42. D.Increase(t, D[k]);
  43. D.Remove(k);
  44. }
  45. private static void Increase(this Dictionary<long, long> D, long t, long v)
  46. {
  47. if (D.ContainsKey(t))
  48. {
  49. D[t] += v;
  50. }
  51. else
  52. {
  53. D.Add(t, v);
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment