Skip to content

Commit

Permalink
feat: solve day 9 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 9, 2024
1 parent 39e68ea commit a3a390b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 4 deletions.
56 changes: 56 additions & 0 deletions src/main/java/com/adventofcode/flashk/day09/DiskDefragmenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.adventofcode.flashk.day09;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class DiskDefragmenter {

private final Deque<File> files = new ArrayDeque<>();
private final List<Unallocated> unallocateds = new ArrayList<>();

public DiskDefragmenter(List<String> inputs){

char[] diskMap = inputs.get(0).toCharArray();
int fileId = 0;
int blockIndex = 0;

for(int i = 0; i < diskMap.length; i++) {

int blockSize = Character.getNumericValue(diskMap[i]);

if(i % 2 == 0) {
files.add(new File(fileId++, blockIndex, blockSize));
} else {
unallocateds.add(new Unallocated(blockIndex, blockSize));
}

blockIndex += blockSize;

}

}

public long solve() {
long result = 0;
while(!files.isEmpty()) {
File file = files.pollLast();
result += reallocate(file);

}
return result;
}

private long reallocate(File file) {

for(Unallocated unallocated : unallocateds) {
if(unallocated.canAllocate(file)) {
unallocated.allocate(file);
break;
}
}

return file.checksum();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public long solveA() {

}

public long solveB() {

return 0;
}
/*
private long checksum() {
long result = 0;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/adventofcode/flashk/day09/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.adventofcode.flashk.day09;

import lombok.Getter;


public class File {

private static int maxIdFile = 0;

private final int id;
@Getter
private int startBlockIndex;
private int endBlockIndex;
@Getter
private final int size;

public File(int fileId, int startBlockIndex, int size) {
this.id = fileId;
this.startBlockIndex = startBlockIndex;
this.endBlockIndex = startBlockIndex + size - 1;
this.size = size;
}

public void move(int startBlockIndex) {
this.startBlockIndex = startBlockIndex;
this.endBlockIndex = startBlockIndex + size - 1;
}

public long checksum() {
long result = 0;
for(int i = startBlockIndex; i <= endBlockIndex; i++) {
result += (long) id * i;
}
return result;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/adventofcode/flashk/day09/Unallocated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.adventofcode.flashk.day09;

import lombok.Getter;

public class Unallocated {

private int startBlockIndex;
private int size;

public Unallocated(int startBlockIndex, int size) {
this.startBlockIndex = startBlockIndex;
this.size = size;
}

public boolean canAllocate(File file) {
return this.size >= file.getSize() && this.startBlockIndex <= file.getStartBlockIndex();
}

public void allocate(File file) {

if(canAllocate(file)) {
file.move(this.startBlockIndex);
this.startBlockIndex += file.getSize();
this.size -= file.getSize();
}
}

}

8 changes: 4 additions & 4 deletions src/test/java/com/adventofcode/flashk/day09/Day09Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public void testSolvePart2Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
DiskFragmenter diskFragmenter = new DiskFragmenter(inputs);
assertEquals(2858L, diskFragmenter.solveB());
DiskDefragmenter diskDefragmenter = new DiskDefragmenter(inputs);
assertEquals(2858L, diskDefragmenter.solve());
}

@Test
Expand All @@ -95,9 +95,9 @@ public void testSolvePart2Input() {
// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

DiskDefragmenter diskDefragmenter = new DiskDefragmenter(inputs);

System.out.println("Solution: ");
assertEquals(0L, 0L);
assertEquals(6335972980679L, diskDefragmenter.solve());

}

Expand Down

0 comments on commit a3a390b

Please sign in to comment.