Skip to content

Commit 75f2a63

Browse files
committed
2024 q8 urgh
1 parent 2404343 commit 75f2a63

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.icecreamhead.adventofcode.q8;
2+
3+
import java.util.LinkedList;
4+
5+
public class DiskFragmenter {
6+
7+
long pt1(String input) {
8+
int[] blocks = generateBlocks(input);
9+
compactBlocks(blocks);
10+
return checksum(blocks);
11+
}
12+
13+
long pt2(String input) {
14+
int[] blocks = generateBlocks(input);
15+
compactFiles(blocks);
16+
return checksum(blocks);
17+
}
18+
19+
static long checksum(int[] blocks) {
20+
int i = 0;
21+
long t = 0;
22+
System.out.printf("length: %d\n", blocks.length);
23+
while (i < blocks.length) {
24+
if (blocks[i] != -1) {
25+
t += blocks[i] * (long) i;
26+
}
27+
i++;
28+
}
29+
return t;
30+
}
31+
32+
static void compactBlocks(int[] blocks) {
33+
int l = 0, r = blocks.length - 1;
34+
while (l < r) {
35+
if (blocks[l] != -1) {
36+
l++;
37+
} else if (blocks[r] == -1) {
38+
r--;
39+
} else {
40+
assert blocks[l] == -1 && blocks[r] != -1;
41+
blocks[l] = blocks[r];
42+
blocks[r] = -1;
43+
}
44+
}
45+
}
46+
47+
static void compactFiles(int[] blocks) {
48+
// System.out.printf("%s%n", new String(blocks));
49+
int r = blocks.length - 1;
50+
while (r >= 0) {
51+
52+
if (blocks[r] == -1) {
53+
r--;
54+
} else {
55+
int rstart = r;
56+
int val = blocks[r];
57+
while (r >= 0 && blocks[r] == val) {
58+
System.out.printf("block[%d] is %d\n", r, blocks[r]);
59+
r--;
60+
}
61+
int rlen = rstart - r;
62+
63+
System.out.printf("len of %d is %d\n", val, rlen);
64+
65+
int emptySpace = findEmptySpaceOfSize(blocks, rlen, r);
66+
System.out.println("empty space: " + emptySpace + " (started at " + 0 + ")");
67+
68+
if (emptySpace != -1) {
69+
for (int i = 0; i < rlen; i++) {
70+
blocks[emptySpace + i] = blocks[r + 1 + i];
71+
blocks[r + 1 + i] = -1;
72+
}
73+
}
74+
// System.out.printf("%s%n", new String(blocks));
75+
}
76+
}
77+
}
78+
79+
private static int findEmptySpaceOfSize(int[] blocks, int reqlen, int max) {
80+
81+
for (int i = 0; i < max; i++) {
82+
83+
if (blocks[i] == -1) {
84+
int blockstart = i;
85+
while (i < max && blocks[i] != -1) {
86+
System.out.printf("block[%d] is %d\n", i, blocks[i]);
87+
i++;
88+
}
89+
int foundlen = 1 + i - blockstart;
90+
System.out.printf("foundlen: %d\n", foundlen);
91+
if (foundlen >= reqlen) return blockstart;
92+
}
93+
94+
}
95+
return -1;
96+
}
97+
98+
static int[] generateBlocks(String input) {
99+
boolean file = true;
100+
int id = 0;
101+
102+
LinkedList<Integer> blocks = new LinkedList<>();
103+
for (char c : input.toCharArray()) {
104+
int repetitions = c - 48;
105+
106+
for (int i = 0; i < repetitions; i++) {
107+
blocks.add(file ? id : -1);
108+
}
109+
if (file) {
110+
id++;
111+
}
112+
file = !file;
113+
}
114+
115+
return blocks.stream().mapToInt(i -> i).toArray();
116+
}
117+
118+
}

0 commit comments

Comments
 (0)