generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/com/adventofcode/flashk/day09/DiskDefragmenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/com/adventofcode/flashk/day09/Unallocated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters