this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 47 lines 1.4 kB view raw
1import { 2 chunk, 3 intersection, 4} from "https://raw.githubusercontent.com/lodash/lodash/4.17.21-es/lodash.js"; 5 6const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 7const valForLetter = (l: string): number => letters.indexOf(l) + 1; 8 9export const splitStr = (str: string): [string, string] => { 10 if (str.length % 2 !== 0) throw `can't split string of length ${str.length}`; 11 return [str.slice(0, str.length / 2), str.slice(str.length / 2)]; 12}; 13 14export const findCommonItem = (s1: string, s2: string): string => { 15 const [a1, a2] = [s1.split(""), s2.split("")]; 16 const found = Array.from(new Set(a1.filter((c: string) => a2.includes(c)))); 17 if (found.length !== 1) { 18 throw `found more than one match: ${ 19 Array.from(found) 20 }; s1: ${s1}; s2: ${s2}`; 21 } 22 return found[0]; 23}; 24 25if (import.meta.main) { 26 const input = await Deno.readTextFile("./input.txt"); 27 const lines = input.trim().split("\n"); 28 29 console.log( 30 "part 1:", 31 lines.map(splitStr).map(([c1, c2]) => findCommonItem(c1, c2)).map( 32 valForLetter, 33 ).reduce((sum, x) => sum + x, 0), 34 ); 35 36 console.log( 37 "part 2:", 38 chunk(lines, 3).map(( 39 [e1, e2, e3], 40 ) => intersection(e1.split(""), e2.split(""), e3.split(""))).map( 41 (common) => { 42 if (common.length > 1) throw `found more than one match: ${common}`; 43 return common[0]; 44 }, 45 ).reduce((sum, x) => sum + valForLetter(x), 0), 46 ); 47}