Advertisement
Guest User

Untitled

a guest
Dec 9th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | Source Code | 0 0
  1. var file = System.IO.File.OpenText("input.txt");
  2. var input = file.ReadToEnd().TrimEnd();
  3.  
  4. var fileId = (ushort) 0;
  5. var memBlocks = new List<ushort?>();
  6. for (int i = 0; i < input.Length; i++)
  7. {
  8.     var cval = input[i] - '0';
  9.     if (i % 2 == 0)
  10.     {
  11.         // file block
  12.         memBlocks.AddRange(Enumerable.Range(0, cval).Select<int, ushort?>(x => fileId));
  13.         fileId++;
  14.     }
  15.     else
  16.     {
  17.         // empty block
  18.         memBlocks.AddRange(Enumerable.Range(0, cval).Select<int, ushort?>(x => null));
  19.     }
  20. }
  21.  
  22. var blocksP1 = memBlocks.ToArray();
  23. var start = 0;
  24. var last = blocksP1.Length - 1;
  25. while (start < last)
  26. {
  27.     while (blocksP1[start] != null)
  28.     {
  29.         start++;
  30.     }
  31.     while (blocksP1[last] == null)
  32.     {
  33.         last--;
  34.     };
  35.     if (start < last)
  36.     {
  37.         blocksP1[start] = blocksP1[last];
  38.         blocksP1[last] = null;
  39.     }
  40. }
  41.  
  42. var p1 = 0ul;
  43. var current = 0;
  44. while (blocksP1[current] != null)
  45. {
  46.     p1 += (ulong)current * blocksP1[current]!.Value;
  47.     current++;
  48. }
  49. Console.WriteLine($"#1: {p1}");
  50.  
  51. var findEmpty = (ushort?[] mem, int lastIndex, int size, string pattern = ".") =>
  52. {
  53.     var s = 0;
  54.     while (s < lastIndex)
  55.     {
  56.         var c = 0;
  57.         while (mem[s + c] == null && c < size && (s + c) < lastIndex) { c++; }
  58.         if (c == size)
  59.         {
  60.             return (s: s, e: s + c - 1);
  61.         }
  62.         s += c > 0 ? c : 1;
  63.     }
  64.     return (s: -1, e: -1);
  65. };
  66.  
  67. var findFilled = (ushort?[] memory, int lastIndex) =>
  68. {
  69.     var e = -1;
  70.     var s = -1;
  71.     var c = lastIndex;
  72.     while (memory[c] == null && c > 0) { c--; }
  73.  
  74.     if (c > 0)
  75.     {
  76.         e = c;
  77.         while (memory[c] == memory[e] && c > 0) { c--; }
  78.         s = c + 1;
  79.     }
  80.     return (s, e);
  81. };
  82.  
  83. var blocksP2 = memBlocks.ToArray();
  84. last = blocksP2.Length - 1;
  85. while (last > 0)
  86. {
  87.     var filled = findFilled(blocksP2, last);
  88.     if (filled.s == -1)
  89.     {
  90.         break;
  91.     }
  92.  
  93.     var empty = findEmpty(blocksP2, filled.s, filled.e - filled.s + 1);
  94.     if (empty.s != -1)
  95.     {
  96.         var fidx = filled.s;
  97.         var eidx = empty.s;
  98.         while (fidx <= filled.e)
  99.         {
  100.             blocksP2[eidx] = blocksP2[fidx];
  101.             blocksP2[fidx] = null;
  102.             fidx++;
  103.             eidx++;
  104.         }
  105.     }
  106.     last = filled.s - 1;
  107. }
  108.  
  109. var p2 = 0ul;
  110. current = 0;
  111. while (current < blocksP2.Length)
  112. {
  113.     p2 += blocksP2[current] != null ? (ulong)current * blocksP2[current]!.Value : 0;
  114.     current++;
  115. }
  116. Console.WriteLine($"#2: {p2}");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement