Skip to content

Commit

Permalink
2024 Day09 Solved - very ugly and slow, but I know what to do to fix …
Browse files Browse the repository at this point in the history
…this
  • Loading branch information
smabuk committed Dec 9, 2024
1 parent 85a206b commit eb4059f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
72 changes: 68 additions & 4 deletions Solutions/2024/Day09.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public partial class Day09 {

public static long Part1(string[] input)
{
string densediskMap = input[0];
List<int> diskMapAsInts = [.. densediskMap.AsDigits<int>()];
string denseDiskMap = input[0];
List<int> diskMapAsInts = [.. denseDiskMap.AsDigits<int>()];
int blockCount = diskMapAsInts.Sum();

List<int> diskMap = new (blockCount);
Expand Down Expand Up @@ -60,12 +60,76 @@ public static long Part1(string[] input)
return checkSum;
}

public static string Part2(string[] input) => NO_SOLUTION_WRITTEN_MESSAGE;
public static long Part2(string[] input)
{
string denseDiskMap = input[0];
List<int> diskMapAsInts = [.. denseDiskMap.AsDigits<int>()];
int blockCount = diskMapAsInts.Sum();

List<Block> diskMap = [];

bool fileOrFreeSpace = FILE;
int blockNo = 0;
int maxBlockNo = 0;
foreach (int item in diskMapAsInts) {
if (fileOrFreeSpace is FILE) {
maxBlockNo = blockNo;
diskMap.Add(new Block(blockNo++, item));
fileOrFreeSpace = FREE_SPACE;
} else {
diskMap.Add(new Block(EMPTY, item));
fileOrFreeSpace = FILE;
}
}


// Defragment
for (blockNo = maxBlockNo; blockNo > 0; blockNo--) {
int blockPtr = diskMap.FindIndex(b => b.Id == blockNo);
Block fileBlock = diskMap[blockPtr];
int firstFreeSpace = diskMap.FindIndex(b => b.Id == EMPTY && b.BlockSize >= fileBlock.BlockSize);
if (blockPtr > firstFreeSpace && firstFreeSpace > 0) {
if (diskMap[blockPtr - 1].Id is EMPTY) {
diskMap[blockPtr - 1] = diskMap[blockPtr - 1] with { BlockSize = diskMap[blockPtr - 1].BlockSize + fileBlock.BlockSize };
diskMap.RemoveAt(blockPtr);
} else {
diskMap[blockPtr] = fileBlock with { Id = EMPTY };
}
Block freeSpaceBlock = diskMap[firstFreeSpace];
diskMap[firstFreeSpace] = freeSpaceBlock with { BlockSize = freeSpaceBlock.BlockSize - fileBlock.BlockSize };
diskMap.Insert(firstFreeSpace, fileBlock);
}
for (int i = diskMap.Count - 1; i > 0; i--) {
if (diskMap[i].Id is EMPTY && diskMap[i - 1].Id is EMPTY) {
diskMap[i - 1] = diskMap[i - 1] with { BlockSize = diskMap[i - 1].BlockSize + diskMap[i].BlockSize };
diskMap.RemoveAt(i);
}

}
}

_ = diskMap.RemoveAll(b => b.BlockSize is 0);

long checkSum = 0;
int idx = 0;
for (int i = 0; i < diskMap.Count; i++) {
if (diskMap[i].Id is not EMPTY) {
for (int j = 0; j < diskMap[i].BlockSize; j++) {
checkSum += diskMap[i].Id * idx++;
}
} else {
idx += diskMap[i].BlockSize;
}
}


return checkSum;
}

private sealed record Block(int id, int BlockSize);
private sealed record Block(int Id, int BlockSize);

private const bool FILE = true;
private const bool FREE_SPACE = false;
private const int EMPTY = int.MinValue;
private const int NOT_FOUND = -1;
}
9 changes: 9 additions & 0 deletions Tests/2024/Tests_09.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public async Task Part1(string input, int expected)
await Task.Delay(500);
}

[Theory]
[InlineData(TEST_DATA, 2858)]
public async Task Part2(string input, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, new Action<string[], bool>(Callback)), out int actual);
actual.ShouldBe(expected);
await Task.Delay(500);
}


private void Callback(string[] lines, bool _)
{
Expand Down

0 comments on commit eb4059f

Please sign in to comment.