From d68ac87ca9b3204562c1794f1330f1c5ac242443 Mon Sep 17 00:00:00 2001 From: Nathan van Ofwegen Date: Sun, 4 Dec 2022 15:32:49 +0000 Subject: [PATCH] d3p2 --- src/2022/day3/index.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/2022/day3/index.ts b/src/2022/day3/index.ts index 608e8be..263aa31 100644 --- a/src/2022/day3/index.ts +++ b/src/2022/day3/index.ts @@ -3,18 +3,28 @@ import { readFile, sum } from "../../utils"; let input = readFile(path.resolve(__dirname, 'input.txt')); -const day1 = input.split("\n").map(line => { - const first = line.substring(0, line.length / 2).split('') - const second = line.substring(line.length / 2).split('') +const day1 = input.split("\n") + .map(line => { + const first = line.substring(0, line.length / 2).split('') + const second = line.substring(line.length / 2).split('') - const both = first.filter(ch => second.includes(ch))[0]; - const ascii = both.charCodeAt(0); - return ascii > 96 ? ascii - 96 : (ascii - 64 + 26); -}).reduce(sum); + const both = first.filter(ch => second.includes(ch))[0]; + const ascii = both.charCodeAt(0); + return ascii > 96 ? ascii - 96 : (ascii - 64 + 26); + }).reduce(sum); -console.log(" Day1:", day1) +console.log("Day1:", day1) -const day2 = input; +const day2 = input.split("\n") + .reduce((a, b, c) => { + if (c % 3 === 0) a.push([]); + a[a.length - 1].push(b); + return a; + }, []).map(group => { + const badge = group[0].split('').filter(ch => group[1].includes(ch) && group[2].includes(ch))[0]; + const ascii = badge.charCodeAt(0); + return ascii > 96 ? ascii - 96 : (ascii - 64 + 26); + }).reduce(sum); console.log("Day2:", day2); \ No newline at end of file