diff --git a/2022/d3/run.sh b/2022/d3/run.sh index 3848496..bd2af83 100755 --- a/2022/d3/run.sh +++ b/2022/d3/run.sh @@ -3,8 +3,9 @@ # Runs deno at src/d3_deno.ts with arguments ["example.txt"] echo "example.txt" deno run --allow-read "${PWD}/src/d3_deno.ts" "${PWD}/data/example.txt" +deno run --allow-read "${PWD}/src/d3_deno_concise.ts" "${PWD}/data/example.txt" echo "submission.txt" deno run --allow-read "${PWD}/src/d3_deno.ts" "${PWD}/data/submission.txt" - +deno run --allow-read "${PWD}/src/d3_deno_concise.ts" "${PWD}/data/submission.txt" diff --git a/2022/d3/src/d3_deno.ts b/2022/d3/src/d3_deno.ts index 182eb05..014bb0c 100755 --- a/2022/d3/src/d3_deno.ts +++ b/2022/d3/src/d3_deno.ts @@ -12,12 +12,6 @@ function priority(chr: string) { : (ord(chr) - ord('A') + 27); } -if (Deno.args.length < 1) { - throw Error( - 'd3_deno.ts expects a single argument that points to AOC\'s day 3 input file', - ); -} - async function main(fileLoc: string) { // part 1 const text = await Deno.readTextFile(fileLoc); @@ -71,6 +65,12 @@ async function main(fileLoc: string) { console.log('part 2:', badges.reduce((a,b)=>a+b)); } +if (Deno.args.length < 1) { + throw Error( + 'd3_deno.ts expects a single argument that points to AOC\'s day 3 input file', + ); +} + const fileLoc = Deno.args[0]; await main(fileLoc); diff --git a/2022/d3/src/d3_deno_concise.ts b/2022/d3/src/d3_deno_concise.ts new file mode 100644 index 0000000..1fc5419 --- /dev/null +++ b/2022/d3/src/d3_deno_concise.ts @@ -0,0 +1,60 @@ +// Usable by Windows and UNIX +const LINE_SPLIT = (/\r?\n/); + +async function main(fileLoc: string) { + const input = await Deno.readTextFile(fileLoc); + const ord = (s: string) => s.charCodeAt(0); + const prioritized = input.split(LINE_SPLIT).map((line) => line.trim()) + .filter((line) => line.length != 0) + .map((line) => + [...line].map((e) => e.charCodeAt(0)) + .map((e) => + e - ((ord('a') <= e && e <= ord('z')) + ? (ord('a') - 1) + : (ord('A') - 27)) + ) + ); + const retain = (a: T[], b: T[]) => { + const bSet = new Set(b); + return a.filter((e) => bSet.has(e)); + }; + const halves = (a: T[]) => + [a.slice(0, a.length / 2), a.slice(a.length / 2, a.length)] as const; + console.log( + 'part 1', + prioritized + .map((sack) => retain(...halves(sack))[0]).reduce((a, b) => a + b), + ); + const arrayGroupBy = (a: T[], groupFn: (elem: T, idx: number) => F) => + a.reduce((ret, e, idx) => { + const identity = groupFn(e, idx); + if (!ret.groupMap.has(identity)) { + ret.groupMap.set(identity, ret.groupKeys.length); + ret.groupKeys.push(identity); + ret.groups.push([identity, []]); + } + ret.groups[ret.groupMap.get(identity)!][1].push(e); + return ret; + }, { + groupKeys: new Array(), + groupMap: new Map(), + groups: new Array<[F, T[]]>(), + }).groups; + + console.log( + 'part 2', + arrayGroupBy(prioritized, (_, idx) => Math.floor(idx / 3)) + .map(([_idx, sacks]) => sacks) + .map((groupOf3) => groupOf3.reduce((a, b) => retain(a, b))[0]) + .reduce((a, b) => a + b), + ); +} + +if (Deno.args.length < 1) { + throw Error( + 'd3_deno.ts expects a single argument that points to AOC\'s day 3 input file', + ); +} + +const fileLoc = Deno.args[0]; +await main(fileLoc);