From 3d57434c04a669610d5f15bd2a7713e6928cdef7 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 30 Nov 2023 22:46:45 -0700 Subject: add aoc2023 --- day-12/sol.ts | 59 ----------------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 day-12/sol.ts (limited to 'day-12/sol.ts') 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; - -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(); -- cgit v1.2.3-70-g09d2