@@ -46,65 +46,47 @@ long long part1(const Data &data)
46
46
auto left = diskmap.begin ();
47
47
auto right = --diskmap.end ();
48
48
while (left != right) {
49
- while (left->first != -1 ) {
49
+ while (left->first != -1 && left != right ) {
50
50
++left;
51
- if (left == right) break ;
52
51
}
53
- if (left == right) break ;
54
- while (right->first == -1 ) {
52
+ while (right->first == -1 && left != right) {
55
53
--right;
56
- if (left == right) break ;
57
54
}
58
55
if (left == right) break ;
56
+
59
57
int remaining_space = left->second ;
60
58
int needed_space = right->second ;
61
59
if (remaining_space > needed_space) {
62
60
// 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
65
61
left->first = right->first ;
66
- // set its size
67
62
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});
72
64
right->first = -1 ;
73
65
}
74
66
else {
75
67
// 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
77
70
left->first = right->first ;
78
- // remove part of the space at the end
79
71
right->second -= remaining_space;
80
- // and add the now blank space to the right
81
72
diskmap.insert (std::next (right), {-1 , remaining_space});
82
73
}
83
-
84
74
// print(diskmap);
85
75
}
86
76
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
- }
98
77
// print(diskmap);
99
78
100
79
long long sum = 0 ;
101
80
left = diskmap.begin ();
102
- // the blank space
103
- right = --diskmap.end ();
104
81
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 ;
108
90
}
109
91
++left;
110
92
}
@@ -124,11 +106,8 @@ long long part2(const Data &data)
124
106
--right;
125
107
}
126
108
while (right != diskmap.begin ()) {
127
- while (left->first != -1 ) {
109
+ while (left->first != -1 && left != right ) {
128
110
++left;
129
- if (left == right) {
130
- break ;
131
- }
132
111
}
133
112
if (left == right) {
134
113
left = diskmap.begin ();
@@ -147,9 +126,7 @@ long long part2(const Data &data)
147
126
if (remaining_space >= needed_space) {
148
127
// there is enough space to entirely fit the right block
149
128
int new_space = remaining_space - needed_space;
150
- // set the id of this block
151
129
left->first = right->first ;
152
- // set its size
153
130
left->second = right->second ;
154
131
// insert new blank space to the right
155
132
if (new_space > 0 ) {
@@ -264,7 +241,7 @@ int main(int argc, char **argv)
264
241
Data data = parse ();
265
242
266
243
long long answer1 = 6349606724455 ;
267
- long long answer2 = 0 ;
244
+ long long answer2 = 6376648986651 ;
268
245
269
246
auto first = part1 (data);
270
247
std::cout << " Part 1: " << first << std::endl;
0 commit comments