-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9_2.groovy
39 lines (35 loc) · 1.23 KB
/
day9_2.groovy
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
def sampleInput = '''2333133121414131402'''
static long sumOfIndexes(int startIndex, int size) {
(2 * startIndex + size - 1) * (size as long) / 2L
}
static long checksumOfChunk(int startIndex, int size, int id) {
sumOfIndexes(startIndex, size) * id
}
static long solve(String input) {
def data = input.trim().collect {it as int }
int endPointer = input.size() % 2 == 1 ? input.size()-1 : input.size()-2
def sizePointer = 0
def checksum = 0
def moved = data.collect { false }
for (int i = 1; i < data.size(); i += 2) {
if (!moved[i-1]) {
checksum += checksumOfChunk(sizePointer, data[i-1], ((i-1)/2) as int)
}
sizePointer += data[i-1]
def emptySpace = data[i]
for (int j = endPointer; j > i; j -= 2) {
if(moved[j]) continue
def candidate = data[j]
if (candidate <= emptySpace) {
checksum += checksumOfChunk(sizePointer, candidate, j/2 as int)
sizePointer += candidate
emptySpace -= candidate
moved[j] = true
}
}
sizePointer += emptySpace
}
checksum
}
assert 2858L == solve(sampleInput)
println solve(new File('input/day9.txt').text)