diff --git a/advent-of-code/day-9.ts b/advent-of-code/day-9.ts new file mode 100644 index 0000000..4e0ffac --- /dev/null +++ b/advent-of-code/day-9.ts @@ -0,0 +1,122 @@ +const real = Deno.readTextFileSync("./day-9.txt").trim(); + +const example = "2333133121414131402"; +const example2 = "233313312141413140212"; // todo this is to test with 10 as id + +// even indexes are sizes of fileblock +// odd indices don't count as ids and are instead markers of `...` between sizes + +const partOne = (input: string) => { + const len = input.length / 2; + + const thing: string[] = []; + let dotsAdded = 0; + for (let i = 0; i < len; i++) { + const [size, freeSpace] = input.slice(i * 2, i * 2 + 2); + + thing.push(...new Array(+size).fill(i)); + if (freeSpace) { + thing.push(...new Array(+freeSpace).fill(".")); + dotsAdded += +freeSpace; + } + } + + let lookingFromEnd = 1; + let lookingFromStart = 1; + // now rebalance + + while (lookingFromEnd <= dotsAdded) { + while (thing.at(-1 * lookingFromEnd) === ".") { + lookingFromEnd++; + } + + const item = thing.at(-lookingFromEnd)!; + while (thing.at(lookingFromStart) !== ".") { + lookingFromStart++; + } + + thing[lookingFromStart] = item; + thing[thing.length - lookingFromEnd] = "."; + + lookingFromEnd++; + lookingFromStart++; + } + + let i = 0; + let result = 0; + while (thing[i] !== ".") { + result += i * +thing[i]; + i++; + } + + while (i < thing.length) { + if (thing[i] !== ".") { + throw new Error(thing[i]); + } + i++; + } + console.log(thing.slice(0, 20)); + console.log(result); +}; + +const partTwo = (input: string) => { + // even indexes are sizes of fileblock + // odd indices don't count as ids and are instead markers of `...` between sizes + + const len = input.length / 2; + + const thing: string[] = []; + let dotsAdded = 0; + for (let i = 0; i < len; i++) { + const [size, freeSpace] = input.slice(i * 2, i * 2 + 2); + + thing.push(...new Array(+size).fill(i)); + if (freeSpace) { + thing.push(...new Array(+freeSpace).fill(".")); + dotsAdded += +freeSpace; + } + } + + for (let i = thing.length - 1; i > 0; i--) { + if (thing[i] === ".") { + continue; + } + const isFirst = i === 0 || thing[i - 1] !== thing[i]; + + if (isFirst) { + let sliceSize = 1; + while (thing[i + sliceSize] === thing[i]) sliceSize++; + const insertIndex = thing + .map((x) => (x === "." ? "." : "x")) + .join("") + .indexOf(".".repeat(sliceSize)); + if (insertIndex < i && insertIndex !== -1) { + thing.splice( + insertIndex, + sliceSize, + ...(new Array(sliceSize).fill(thing[i]) as string[]) + ); + thing.splice( + i, + sliceSize, + ...(new Array(sliceSize).fill(".") as string[]) + ); + } + } + } + + console.log( + thing.reduce((acc, item, ind) => { + if (item === ".") return acc; + + return acc + Number(item) * ind; + }, 0) + ); +}; + +partOne(real); +partTwo(real); + +// 6378826667552 + +// my problem is for numbers bigger than 10 we need to still group them as a single id diff --git a/input.txt b/advent-of-code/day-9.txt similarity index 100% rename from input.txt rename to advent-of-code/day-9.txt diff --git a/main.ts b/main.ts index f731ddb..81ab89b 100644 --- a/main.ts +++ b/main.ts @@ -1,74 +1 @@ -const real = Deno.readTextFileSync("./input.txt").trim(); - -const example = "2333133121414131402"; -const example2 = "233313312141413140212"; // todo this is to test with 10 as id - -// even indexes are sizes of fileblock -// odd indices don't count as ids and are instead markers of `...` between sizes - -const getCheckSum = (input: string) => { - const len = input.length / 2; - - let thing = ""; - let dotsAdded = 0; - for (let i = 0; i < len; i++) { - const [size, freeSpace] = input.slice(i * 2, i * 2 + 2); - const sizeString = new Array(+size).fill(i).join(""); - - thing += sizeString; - if (freeSpace) { - const freeString = new Array(+freeSpace).fill(".").join(""); - dotsAdded += +freeSpace; - thing += freeString; - } - } - - const resultThing = thing.split(""); - - let lookingFromEnd = 1; - let lookingFromStart = 1; - // now rebalance - - while (lookingFromEnd <= dotsAdded) { - while (resultThing.at(-1 * lookingFromEnd) === ".") { - lookingFromEnd++; - } - - const item = resultThing.at(-lookingFromEnd)!; - while (resultThing.at(lookingFromStart) !== ".") { - lookingFromStart++; - } - - resultThing[lookingFromStart] = item; - resultThing[resultThing.length - lookingFromEnd] = "."; - - lookingFromEnd++; - lookingFromStart++; - } - - let i = 0; - let result = 0; - while (resultThing[i] !== ".") { - result += i * +resultThing[i]; - i++; - } - - while (i < resultThing.length) { - if (resultThing[i] !== ".") { - throw new Error(resultThing[i]); - } - i++; - } - console.log({ - dotsAdded, - lookingFromEnd, - lookingFromStart, - }); - console.log(result); -}; - -getCheckSum(real); - -// 90328963761 - -// my problem is for numbers bigger than 10 we need to still group them as a single id +// hi