From a3a390ba684774b8ccd83d678c9a49194714c86f Mon Sep 17 00:00:00 2001 From: Flashky Date: Mon, 9 Dec 2024 08:58:08 +0100 Subject: [PATCH] feat: solve day 9 part 2 --- .../flashk/day09/DiskDefragmenter.java | 56 +++++++++++++++++++ .../flashk/day09/DiskFragmenter.java | 4 ++ .../com/adventofcode/flashk/day09/File.java | 36 ++++++++++++ .../flashk/day09/Unallocated.java | 29 ++++++++++ .../adventofcode/flashk/day09/Day09Test.java | 8 +-- 5 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/adventofcode/flashk/day09/DiskDefragmenter.java create mode 100644 src/main/java/com/adventofcode/flashk/day09/File.java create mode 100644 src/main/java/com/adventofcode/flashk/day09/Unallocated.java diff --git a/src/main/java/com/adventofcode/flashk/day09/DiskDefragmenter.java b/src/main/java/com/adventofcode/flashk/day09/DiskDefragmenter.java new file mode 100644 index 0000000..c816409 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day09/DiskDefragmenter.java @@ -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 files = new ArrayDeque<>(); + private final List unallocateds = new ArrayList<>(); + + public DiskDefragmenter(List 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(); + } +} diff --git a/src/main/java/com/adventofcode/flashk/day09/DiskFragmenter.java b/src/main/java/com/adventofcode/flashk/day09/DiskFragmenter.java index 175760b..fabe30a 100644 --- a/src/main/java/com/adventofcode/flashk/day09/DiskFragmenter.java +++ b/src/main/java/com/adventofcode/flashk/day09/DiskFragmenter.java @@ -58,6 +58,10 @@ public long solveA() { } + public long solveB() { + + return 0; + } /* private long checksum() { long result = 0; diff --git a/src/main/java/com/adventofcode/flashk/day09/File.java b/src/main/java/com/adventofcode/flashk/day09/File.java new file mode 100644 index 0000000..caf1b51 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day09/File.java @@ -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; + } +} diff --git a/src/main/java/com/adventofcode/flashk/day09/Unallocated.java b/src/main/java/com/adventofcode/flashk/day09/Unallocated.java new file mode 100644 index 0000000..73117d6 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day09/Unallocated.java @@ -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(); + } + } + +} + diff --git a/src/test/java/com/adventofcode/flashk/day09/Day09Test.java b/src/test/java/com/adventofcode/flashk/day09/Day09Test.java index c380d82..8fc8900 100644 --- a/src/test/java/com/adventofcode/flashk/day09/Day09Test.java +++ b/src/test/java/com/adventofcode/flashk/day09/Day09Test.java @@ -81,8 +81,8 @@ public void testSolvePart2Sample() { // Read input file List 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 @@ -95,9 +95,9 @@ public void testSolvePart2Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); + DiskDefragmenter diskDefragmenter = new DiskDefragmenter(inputs); - System.out.println("Solution: "); - assertEquals(0L, 0L); + assertEquals(6335972980679L, diskDefragmenter.solve()); }