diff --git a/AoCCore/SampleData/Day09.txt b/AoCCore/SampleData/Day09.txt index e69de29..5ff5aae 100644 --- a/AoCCore/SampleData/Day09.txt +++ b/AoCCore/SampleData/Day09.txt @@ -0,0 +1 @@ +2333133121414131402 \ No newline at end of file diff --git a/AoCCore/SampleData/Day10.txt b/AoCCore/SampleData/Day10.txt new file mode 100644 index 0000000..e69de29 diff --git a/AoCCore/Y2024.cs b/AoCCore/Y2024.cs index 9008603..8aab5b1 100644 --- a/AoCCore/Y2024.cs +++ b/AoCCore/Y2024.cs @@ -789,7 +789,83 @@ bool Mark(Dot a1, Dot a2, int mul) // 1233 } - public static void Current() // Day09A() + // 2333133121414131402 + public static void Day09A() + { + var input = Read.Line().Select(x => x - '0'); + + var map = input.SelectIndex().Select(x => Enumerable.Repeat(x.Index % 2 == 0 ? x.Index / 2 : -1, x.Value).ToArray()).SelectMany(x => x).ToArray(); + + int i = -1; + int j = map.Length; + + while (++i < j) + { + if (map[i] != -1) continue; + + while (map[--j] == -1) continue; + + map[i] = map[j]; + map[j] = -1; + } + + map.SelectIndex().Where(x => x.Value != -1).Sum(x => (long)x.Index * x.Value).P(); + } + + public static void Day09B() + { + var input = Read.Line().Select(x => x - '0').ToArray(); + + var map = input.SelectIndex().Select(x => Enumerable.Repeat(x.Index % 2 == 0 ? x.Index / 2 : -1, x.Value).ToArray()).SelectMany(x => x).ToArray(); + List<(int Position, int Size)> freeMap = []; + + int position = 0; + for (int k = 0; k < input.Length; k++) + { + if (k % 2 != 0) + { + freeMap.Add((position, input[k])); + } + + position += input[k]; + } + + int backLocation = map.Length; + for (int j = input.Length - 1; j >= 0; j--) + { + backLocation -= input[j]; + if (j % 2 != 0) continue; + + var size = input[j]; + + for (int k = 0; k < freeMap.Count; k++) + { + var location = freeMap[k].Position; + + if (location >= backLocation) break; + if (freeMap[k].Size < size) continue; + + for (int index = 0; index < size; index++) + { + if (map[location + index] != -1) throw new Exception(); + if (map[backLocation + index] == -1) throw new Exception(); + + map[location + index] = j / 2; + map[backLocation + index] = -1; + } + + freeMap[k] = (location + size, freeMap[k].Size - size); + + break; + } + } + + map.SelectIndex().Where(x => x.Value != -1).Sum(x => (long)x.Index * x.Value).P(); + + // 6373055193464 + } + + public static void Current() // Day10A() { }