diff --git a/09/index.ts b/09/index.ts index 146dcee..4c272cc 100644 --- a/09/index.ts +++ b/09/index.ts @@ -102,6 +102,7 @@ while (rightCursor > 0) { ); if (target.index < Infinity && target.index < rightCursor) { // slot exists; move the file, update the array, and update the space lists + // but only if the new location is actually to the right of our cursor sparceArray.splice( target.index, fileSize, @@ -114,11 +115,9 @@ while (rightCursor > 0) { ); spaces[target.size].shift(); if (toNum(target.size) - fileSize > 0) { - spaces[(toNum(target.size) - fileSize).toString()].unshift( - target.index + fileSize - ); - spaces[(toNum(target.size) - fileSize).toString()].sort((a, b) => - a > b ? 1 : -1 + let newGap = (toNum(target.size) - fileSize).toString(); + spaces[newGap] = [...spaces[newGap], target.index + fileSize].sort( + (a, b) => (a > b ? 1 : -1) ); } }