d3: initial sub

master
pegasust 2022-12-03 07:51:40 +00:00
parent b45dc32c74
commit b5c22d2126
1 changed files with 26 additions and 0 deletions

View File

@ -4,3 +4,29 @@ if(Deno.args.length < 1) {
const fileLoc = Deno.args[0];
const text = await Deno.readTextFile(fileLoc);
// Usable by Windows and UNIX
const LINE_SPLIT = (/\r?\n/);
function priority(chr: string) {
if(chr.length != 1) {
throw Error("Expecting character");
}
const ord = (v: string) => v.charCodeAt(0);
return (ord('a') <= ord(chr) && ord(chr) <= ord('z'))? (ord(chr) - ord('a') + 1):
(ord(chr) - ord('A') + 1);
}
const sumPrioOfShared = text.split(LINE_SPLIT) // split by each line, basically iterates through each rucksack
.map(s=>[...s].map(priority)) // translate each character into respective priority
.map((v)=> [v.slice(0, v.length/2), v.slice(v.length/2, v.length) ]) // splits by 2 compartments
.map(([left, right])=> {
// Finds the value on the right compartment that also exists in left compartment
const left_set = new Set(left);
// by the prompt, we're pretty guaranteed to have a solution
const shared = right.find(e => left_set.has(e))!;
console.log("shared:", shared);
return shared;
}).reduce((a,b)=>a+b); // find its sum
console.log(sumPrioOfShared);