Skip to content

Commit b63cff3

Browse files
committed
a little better
1 parent 9c65d27 commit b63cff3

File tree

1 file changed

+16
-39
lines changed

1 file changed

+16
-39
lines changed

2024/src/day9.cpp

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,65 +46,47 @@ long long part1(const Data &data)
4646
auto left = diskmap.begin();
4747
auto right = --diskmap.end();
4848
while (left != right) {
49-
while(left->first != -1) {
49+
while(left->first != -1 && left != right) {
5050
++left;
51-
if (left == right) break;
5251
}
53-
if (left == right) break;
54-
while(right->first == -1) {
52+
while(right->first == -1 && left != right) {
5553
--right;
56-
if (left == right) break;
5754
}
5855
if (left == right) break;
56+
5957
int remaining_space = left->second;
6058
int needed_space = right->second;
6159
if (remaining_space > needed_space) {
6260
// there is enough space to entirely fit the right block
63-
int new_space = remaining_space - needed_space;
64-
// set the id of this block
6561
left->first = right->first;
66-
// set its size
6762
left->second = right->second;
68-
// insert new blank space to the right
69-
diskmap.insert(std::next(left), {-1, new_space});
70-
71-
// the block at the end is now unallocated
63+
diskmap.insert(std::next(left), {-1, remaining_space - needed_space});
7264
right->first = -1;
7365
}
7466
else {
7567
// we can only partially fill this block
76-
// assign the id
68+
// move some of the block on the right to the left
69+
// left's space will be entirely filled and doesn't need to change
7770
left->first = right->first;
78-
// remove part of the space at the end
7971
right->second -= remaining_space;
80-
// and add the now blank space to the right
8172
diskmap.insert(std::next(right), {-1, remaining_space});
8273
}
83-
8474
// print(diskmap);
8575
}
8676

87-
// now combine all of the empty spaces
88-
while (left->first != -1) {
89-
++left;
90-
}
91-
right = std::next(left);
92-
while(right != diskmap.end()) {
93-
left->second += right->second;
94-
auto temp = std::next(right);
95-
diskmap.erase(right);
96-
right = temp;
97-
}
9877
// print(diskmap);
9978

10079
long long sum = 0;
10180
left = diskmap.begin();
102-
// the blank space
103-
right = --diskmap.end();
10481
int i = 0;
105-
while(left != right) {
106-
for(int j = 0; j < left->second; ++j, ++i) {
107-
sum += left->first * i;
82+
while(left != diskmap.end()) {
83+
if (left->first != -1) {
84+
for(int j = 0; j < left->second; ++j, ++i) {
85+
sum += left->first * i;
86+
}
87+
}
88+
else {
89+
i += left->second;
10890
}
10991
++left;
11092
}
@@ -124,11 +106,8 @@ long long part2(const Data &data)
124106
--right;
125107
}
126108
while (right != diskmap.begin()) {
127-
while(left->first != -1) {
109+
while(left->first != -1 && left != right) {
128110
++left;
129-
if (left == right) {
130-
break;
131-
}
132111
}
133112
if (left == right) {
134113
left = diskmap.begin();
@@ -147,9 +126,7 @@ long long part2(const Data &data)
147126
if (remaining_space >= needed_space) {
148127
// there is enough space to entirely fit the right block
149128
int new_space = remaining_space - needed_space;
150-
// set the id of this block
151129
left->first = right->first;
152-
// set its size
153130
left->second = right->second;
154131
// insert new blank space to the right
155132
if (new_space > 0) {
@@ -264,7 +241,7 @@ int main(int argc, char **argv)
264241
Data data = parse();
265242

266243
long long answer1 = 6349606724455;
267-
long long answer2 = 0;
244+
long long answer2 = 6376648986651;
268245

269246
auto first = part1(data);
270247
std::cout << "Part 1: " << first << std::endl;

0 commit comments

Comments
 (0)