-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC092024.java
146 lines (127 loc) Β· 4.05 KB
/
AoC092024.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.adventofcode.aoc2024;
import static com.adventofcode.utils.Utils.charToInt;
import static com.adventofcode.utils.Utils.getFirstString;
import static com.adventofcode.utils.Utils.itoa;
import com.adventofcode.Solution;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
class AoC092024 implements Solution {
private static final int FREE_ID = -1;
@Override
public String solveFirstPart(final Stream<String> input) {
return solve( input, true );
}
@Override
public String solveSecondPart(final Stream<String> input) {
return solve( input, false );
}
private String solve(final Stream<String> input, final boolean first) {
List<Block> freeBlocks = new ArrayList<>();
List<Block> fileBlocks = new ArrayList<>();
initializeBlocks( getFirstString( input ).toCharArray(), freeBlocks, fileBlocks );
List<Block> movedBlocks = new ArrayList<>();
//iterate file blocks from the last
for ( int i = fileBlocks.size() - 1; i >= 0; i-- ) {
Block fileBlock = fileBlocks.get( i );
//iterate free blocks from the first
boolean fullFileMoved = false;
for ( Iterator<Block> freeBlocksItr = freeBlocks.iterator();
freeBlocksItr.hasNext() && !fullFileMoved; ) {
Block freeBlock = freeBlocksItr.next();
if ( freeBlock.start > fileBlock.start ) {
//crossed free blocks
break;
}
int movedBlockStart = freeBlock.start;
int movedBlockEnd = movedBlockStart + fileBlock.size();
if ( freeBlock.size() == fileBlock.size() ) {
fullFileMoved = true;
freeBlocksItr.remove();
} else if ( freeBlock.size() > fileBlock.size() ) {
fullFileMoved = true;
//resize free block
freeBlock.setStart( movedBlockEnd );
} else if ( first ) {
//move file partially
movedBlockEnd = freeBlock.end;
fileBlock.setEnd( fileBlock.end - freeBlock.size() );
freeBlocksItr.remove();
}
if ( fullFileMoved || first ) {
movedBlocks.add( new Block( fileBlock.id, movedBlockStart, movedBlockEnd ) );
}
}
if ( !fullFileMoved ) {
//keep file in the same position
movedBlocks.add( fileBlock );
} else if ( !first ) {
freeBlocks.add( new Block( FREE_ID, fileBlock.start, fileBlock.end ) );
mergeBlocks( freeBlocks );
}
}
return itoa( movedBlocks.stream().mapToLong( Block::computeCheckSum ).sum() );
}
private void initializeBlocks(final char[] disk, final List<Block> freeBlocks,
final List<Block> fileBlocks) {
int id = 0;
int start = 0;
for ( int i = 0; i < disk.length; i++ ) {
int size = charToInt( disk[i] );
int end = start + size;
if ( i % 2 != 0 ) {
freeBlocks.add( new Block( FREE_ID, start, end ) );
} else {
fileBlocks.add( new Block( id++, start, end ) );
}
start = end;
}
}
private void mergeBlocks(final List<Block> freeBlocks) {
if ( freeBlocks.isEmpty() ) {
return;
}
Iterator<Block> iterator = freeBlocks.iterator();
Block prev = iterator.next();
while ( iterator.hasNext() ) {
Block curr = iterator.next();
if ( prev.end == curr.start ) {
prev.setEnd( curr.end );
iterator.remove();
} else {
prev = curr;
}
}
}
private static final class Block {
final int id;
int start;
int end;
private Block(int id, int start, int end) {
this.id = id;
this.start = start;
this.end = end;
}
void setStart(int start) {
this.start = start;
}
void setEnd(int end) {
this.end = end;
}
int size() {
return end - start;
}
public long computeCheckSum() {
long result = 0;
for ( long j = start; j < end; j++ ) {
result += (id * j);
}
return result;
}
@Override
public String toString() {
return "id: " + id + " block: [" + start + ", " + end + "] size: " + size();
}
}
}