Skip to content

Commit a3a390b

Browse files
committed
feat: solve day 9 part 2
1 parent 39e68ea commit a3a390b

File tree

5 files changed

+129
-4
lines changed

5 files changed

+129
-4
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.adventofcode.flashk.day09;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
public class DiskDefragmenter {
9+
10+
private final Deque<File> files = new ArrayDeque<>();
11+
private final List<Unallocated> unallocateds = new ArrayList<>();
12+
13+
public DiskDefragmenter(List<String> inputs){
14+
15+
char[] diskMap = inputs.get(0).toCharArray();
16+
int fileId = 0;
17+
int blockIndex = 0;
18+
19+
for(int i = 0; i < diskMap.length; i++) {
20+
21+
int blockSize = Character.getNumericValue(diskMap[i]);
22+
23+
if(i % 2 == 0) {
24+
files.add(new File(fileId++, blockIndex, blockSize));
25+
} else {
26+
unallocateds.add(new Unallocated(blockIndex, blockSize));
27+
}
28+
29+
blockIndex += blockSize;
30+
31+
}
32+
33+
}
34+
35+
public long solve() {
36+
long result = 0;
37+
while(!files.isEmpty()) {
38+
File file = files.pollLast();
39+
result += reallocate(file);
40+
41+
}
42+
return result;
43+
}
44+
45+
private long reallocate(File file) {
46+
47+
for(Unallocated unallocated : unallocateds) {
48+
if(unallocated.canAllocate(file)) {
49+
unallocated.allocate(file);
50+
break;
51+
}
52+
}
53+
54+
return file.checksum();
55+
}
56+
}

src/main/java/com/adventofcode/flashk/day09/DiskFragmenter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public long solveA() {
5858

5959
}
6060

61+
public long solveB() {
62+
63+
return 0;
64+
}
6165
/*
6266
private long checksum() {
6367
long result = 0;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.adventofcode.flashk.day09;
2+
3+
import lombok.Getter;
4+
5+
6+
public class File {
7+
8+
private static int maxIdFile = 0;
9+
10+
private final int id;
11+
@Getter
12+
private int startBlockIndex;
13+
private int endBlockIndex;
14+
@Getter
15+
private final int size;
16+
17+
public File(int fileId, int startBlockIndex, int size) {
18+
this.id = fileId;
19+
this.startBlockIndex = startBlockIndex;
20+
this.endBlockIndex = startBlockIndex + size - 1;
21+
this.size = size;
22+
}
23+
24+
public void move(int startBlockIndex) {
25+
this.startBlockIndex = startBlockIndex;
26+
this.endBlockIndex = startBlockIndex + size - 1;
27+
}
28+
29+
public long checksum() {
30+
long result = 0;
31+
for(int i = startBlockIndex; i <= endBlockIndex; i++) {
32+
result += (long) id * i;
33+
}
34+
return result;
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.adventofcode.flashk.day09;
2+
3+
import lombok.Getter;
4+
5+
public class Unallocated {
6+
7+
private int startBlockIndex;
8+
private int size;
9+
10+
public Unallocated(int startBlockIndex, int size) {
11+
this.startBlockIndex = startBlockIndex;
12+
this.size = size;
13+
}
14+
15+
public boolean canAllocate(File file) {
16+
return this.size >= file.getSize() && this.startBlockIndex <= file.getStartBlockIndex();
17+
}
18+
19+
public void allocate(File file) {
20+
21+
if(canAllocate(file)) {
22+
file.move(this.startBlockIndex);
23+
this.startBlockIndex += file.getSize();
24+
this.size -= file.getSize();
25+
}
26+
}
27+
28+
}
29+

src/test/java/com/adventofcode/flashk/day09/Day09Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public void testSolvePart2Sample() {
8181

8282
// Read input file
8383
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
84-
DiskFragmenter diskFragmenter = new DiskFragmenter(inputs);
85-
assertEquals(2858L, diskFragmenter.solveB());
84+
DiskDefragmenter diskDefragmenter = new DiskDefragmenter(inputs);
85+
assertEquals(2858L, diskDefragmenter.solve());
8686
}
8787

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

98+
DiskDefragmenter diskDefragmenter = new DiskDefragmenter(inputs);
9899

99-
System.out.println("Solution: ");
100-
assertEquals(0L, 0L);
100+
assertEquals(6335972980679L, diskDefragmenter.solve());
101101

102102
}
103103

0 commit comments

Comments
 (0)