|
| 1 | +import { getPuzzle } from "../../utils"; |
| 2 | + |
| 3 | +const puzzleInput = getPuzzle(__dirname).trim(); |
| 4 | +const diskMap = puzzleInput.split(""); |
| 5 | + |
| 6 | +// Part 1 |
| 7 | +(() => { |
| 8 | + console.time("part 1"); |
| 9 | + let representation = ""; |
| 10 | + |
| 11 | + for (let i = 0; i < diskMap.length; i++) { |
| 12 | + const entry = Number(diskMap[i]); |
| 13 | + const isFile = i % 2 === 0; |
| 14 | + |
| 15 | + for (let j = 0; j < entry; j++) { |
| 16 | + representation += `${isFile ? i / 2 : "."}_`; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + let representationArr = representation |
| 21 | + .split("_") |
| 22 | + .filter((e) => e !== "") |
| 23 | + .map((e) => (e === "." ? e : Number(e))); |
| 24 | + |
| 25 | + let firstFreeSpaceBlockIndex = representationArr.indexOf("."); |
| 26 | + let lastFileBlockIndex = representationArr.findLastIndex((e) => e !== "."); |
| 27 | + |
| 28 | + while (lastFileBlockIndex > firstFreeSpaceBlockIndex) { |
| 29 | + const lastEntry = representationArr.pop(); |
| 30 | + |
| 31 | + if (lastEntry !== ".") { |
| 32 | + representationArr[firstFreeSpaceBlockIndex] = lastEntry; |
| 33 | + } |
| 34 | + |
| 35 | + firstFreeSpaceBlockIndex = representationArr.indexOf("."); |
| 36 | + lastFileBlockIndex = representationArr.findLastIndex((e) => e !== "."); |
| 37 | + |
| 38 | + if (firstFreeSpaceBlockIndex === -1) { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + let checksum = 0; |
| 44 | + |
| 45 | + for (let i = 0; i < representationArr.length; i++) { |
| 46 | + const block = representationArr[i]; |
| 47 | + |
| 48 | + if (block !== ".") { |
| 49 | + checksum += block * i; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + console.log("part 1 checksum ::", checksum); |
| 54 | + console.timeEnd("part 1"); |
| 55 | +})(); |
| 56 | + |
| 57 | +// Part 2 |
| 58 | +(() => { |
| 59 | + console.time("part 2"); |
| 60 | + let representation = ""; |
| 61 | + |
| 62 | + for (let i = 0; i < diskMap.length; i++) { |
| 63 | + const entry = Number(diskMap[i]); |
| 64 | + const isFile = i % 2 === 0; |
| 65 | + |
| 66 | + for (let j = 0; j < entry; j++) { |
| 67 | + representation += `${isFile ? i / 2 : "."}_`; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + let representationArr = representation |
| 72 | + .split("_") |
| 73 | + .filter((e) => e !== "") |
| 74 | + .map((e) => (e === "." ? e : Number(e))); |
| 75 | + |
| 76 | + const checkedNumbers = new Set<number>(); |
| 77 | + |
| 78 | + let i = representationArr.length; |
| 79 | + |
| 80 | + while (true) { |
| 81 | + i = i - 1; |
| 82 | + |
| 83 | + if (i <= 0) { |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + const num = representationArr[i]; |
| 88 | + |
| 89 | + if (num === "." || checkedNumbers.has(num)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + checkedNumbers.add(num); |
| 94 | + |
| 95 | + if (num === 0) { |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + let fileSize = 1; |
| 100 | + let j = i; |
| 101 | + |
| 102 | + while (true) { |
| 103 | + j = j - 1; |
| 104 | + if (representationArr[j] === num) { |
| 105 | + fileSize++; |
| 106 | + } else { |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + const availableSpace = representationArr.findIndex((entry, i, arr) => { |
| 112 | + if (entry === ".") { |
| 113 | + let match = true; |
| 114 | + |
| 115 | + for (let k = 0; k < fileSize; k++) { |
| 116 | + if (arr[i + k] !== ".") { |
| 117 | + match = false; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return match; |
| 122 | + } |
| 123 | + |
| 124 | + return false; |
| 125 | + }); |
| 126 | + |
| 127 | + if (availableSpace === -1 || availableSpace > j) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + for (let k = 0; k < fileSize; k++) { |
| 132 | + representationArr[availableSpace + k] = num; |
| 133 | + |
| 134 | + representationArr[j + 1 + k] = "."; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + let checksum = 0; |
| 139 | + |
| 140 | + for (let i = 0; i < representationArr.length; i++) { |
| 141 | + const block = representationArr[i]; |
| 142 | + |
| 143 | + if (block !== ".") { |
| 144 | + checksum += block * i; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + console.log("part 2 checksum ::", checksum); |
| 149 | + console.timeEnd("part 2"); |
| 150 | +})(); |
0 commit comments