-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.java
117 lines (107 loc) · 3.28 KB
/
Day9.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.IO;
import com.horstmann.adventofcode.*;
record Block(int id, int start, int length, int free) {
long checksum() {
long sum = 0;
for (int i = 0; i < length; i++)
sum += id * (start + i);
return sum;
}
}
List<Block> blocks;
int pos;
long sum;
void parse(Path p) throws IOException {
String input = Files.readString(p);
blocks = new ArrayList<>();
int start = 0;
for (int i = 0; i < input.length(); i += 2) {
int length = input.charAt(i) - '0';
if (i == input.length() - 1) blocks.add(new Block(i / 2, start, length, 0));
else {
int free = input.charAt(i + 1) - '0';
blocks.add(new Block(i / 2, start, length, free));
start += length + free;
}
}
}
void checksum(int id, int length) {
for (int i = 0; i < length; i++)
sum += (pos + i) * id;
pos += length;
}
Object part1() {
sum = 0;
pos = 0;
int f = 0;
int b = blocks.size() - 1;
Block back = blocks.get(b);
int frontFree = 0;
int backLength = back.length;
while (f < b) {
Block front = blocks.get(f);
frontFree = front.free;
checksum(front.id, front.length);
// fill free area
boolean filled = false;
while (!filled) {
if (backLength <= frontFree) { // back fits
checksum(back.id, backLength);
if (backLength < frontFree) { // space for more
frontFree -= backLength;
}
else { // fits exactly
filled = true;
}
b--;
if (b == f) {
filled = true;
backLength = 0;
} else {
back = blocks.get(b);
backLength = back.length;
}
} else { // back too large
checksum(back.id, frontFree);
backLength -= frontFree;
filled = true;
}
}
f++;
}
checksum(back.id, backLength);
return sum;
}
Object part2() {
int b = blocks.size() - 1;
int lastMoved = b + 1;
while (b > 0) {
Block back = blocks.get(b);
boolean moved = false;
if (back.id() < lastMoved) { // Don't move twice
for (int f = 0; !moved && f < b; f++) {
// Find first free space that can hold the entire file
Block front = blocks.get(f);
if (front.free >= back.length) {
blocks.remove(b);
lastMoved = b;
blocks.set(f, new Block(front.id, front.start, front.length, 0));
blocks.add(f + 1, new Block(back.id, front.start + front.length, back.length, front.free - back.length));
moved = true;
}
}
}
if (!moved) b--; // leave at its place
}
return blocks.stream().mapToLong(Block::checksum).sum();
}
void main() throws IOException {
Util.time(() -> {
parse(Util.inputPath("a"));
IO.println(part1());
IO.println(part2());
parse(Util.inputPath("z"));
IO.println(part1());
IO.println(part2());
});
}