diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-11-30 22:46:45 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-11-30 22:46:45 -0700 |
commit | 3d57434c04a669610d5f15bd2a7713e6928cdef7 (patch) | |
tree | a0f1f04a335bbc808369d6492f4fee2ff06a0bdb /day-12/sol.ts | |
parent | 59966ade163a39fc03f07a9d905e0bd87a98d60c (diff) | |
download | aoc-3d57434c04a669610d5f15bd2a7713e6928cdef7.tar.gz aoc-3d57434c04a669610d5f15bd2a7713e6928cdef7.zip |
add aoc2023
Diffstat (limited to 'day-12/sol.ts')
-rw-r--r-- | day-12/sol.ts | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/day-12/sol.ts b/day-12/sol.ts deleted file mode 100644 index 7143403..0000000 --- a/day-12/sol.ts +++ /dev/null @@ -1,59 +0,0 @@ -import * as input from "fs"; - -type NestedNumbers = Array<NestedNumbers | number>; - -const compare = ( - a: NestedNumbers | number, - b: NestedNumbers | number -): boolean => { - if (typeof a === typeof b && typeof b === "number") return a < b; - if (Array.isArray(a) && Array.isArray(b)) { - for (let i = 0; i < a.length; i++) { - if (i >= b.length) return false; - if (compare(a[i], b[i])) return true; - if (compare(b[i], a[i])) return false; - } - return compare(a.length, b.length); - } - return compare(Array.isArray(b) ? [a] : a, Array.isArray(a) ? [b] : b); -}; - -const main = (): void => { - const lines: NestedNumbers[] = input - .readFileSync("input", "utf-8") - .split("\n") - .filter((x) => x) - .map((x) => JSON.parse(x)); - - const pairs: [NestedNumbers, NestedNumbers][] = []; - for (let i = 0; i < lines.length; i += 2) { - const pair: [NestedNumbers, NestedNumbers] = [lines[i], lines[i + 1]]; - pairs.push(pair); - } - - console.log( - pairs.reduce((acc, [a, b], i) => acc + (compare(a, b) ? i + 1 : 0), 0) - ); - - lines.push([[2]]); - lines.push([[6]]); - - const sorted = lines.sort((a, b) => { - if (compare(a, b)) return -1; - if (compare(b, a)) return 1; - return 0; - }); - - const isPacket = (num: number) => (x: NestedNumbers) => - Array.isArray(x) && - x.length === 1 && - Array.isArray(x[0]) && - x[0].length === 1 && - x[0][0] === num; - - console.log( - (sorted.findIndex(isPacket(6)) + 1) * (sorted.findIndex(isPacket(2)) + 1) - ); -}; - -main(); |