48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
const input = Deno.readTextFileSync('./input.txt').split('\n');
|
|
|
|
function getPrio(item: string): number {
|
|
const itemVal = item.codePointAt(0)!;
|
|
// Split upper and lowercase
|
|
// lowercase
|
|
if (itemVal > 90) return itemVal - 96;
|
|
// uppercase
|
|
return itemVal - 38;
|
|
}
|
|
|
|
function findDup(left: Set<string>, ...right: Set<string>[]): string {
|
|
for (const char of left) {
|
|
if (right.every((rucksack) => rucksack.has(char))) return char;
|
|
}
|
|
// This should never happen
|
|
return '';
|
|
}
|
|
|
|
// Part 1
|
|
let score = 0;
|
|
for (const line of input) {
|
|
if (line === '') continue;
|
|
const halfLength = line.length / 2;
|
|
const left = new Set(line.slice(0, halfLength));
|
|
const right = new Set(line.slice(halfLength));
|
|
const char = findDup(left, right);
|
|
score += getPrio(char);
|
|
}
|
|
|
|
console.log('P1 Sum of priorities:\t', score);
|
|
|
|
// Part 2
|
|
score = 0;
|
|
let index = 0;
|
|
// skip last empty line
|
|
const length = input.length - 1;
|
|
while (index < length) {
|
|
const bp1 = new Set(input[index]);
|
|
const bp2 = new Set(input[index + 1]);
|
|
const bp3 = new Set(input[index + 2]);
|
|
const char = findDup(bp1, bp2, bp3);
|
|
score += getPrio(char);
|
|
index += 3;
|
|
}
|
|
|
|
console.log('P2 Sum of priorities:\t', score);
|