Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 3, 2022 05:06
import { Advent, f } from "advent";
import { Set } from "immutable";
const { compute, computeCheck } = await Advent({ day: 3 });
// compute(1, async (input) => {
// const data = input.parse(f.nl(f.chrs()));
// let ans = 0;
// for (const line of data) {
// let firstHalf = line.slice(0, line.length / 2);
// let secondHalf = line.slice(line.length / 2);
// const f = Set(firstHalf);
// const s = Set(secondHalf);
// const intersection = f.intersect(s);
// for (const c of intersection) {
// if (c.toLowerCase() === c) {
// ans += c.charCodeAt(0) - "a".charCodeAt(0) + 1;
// } else {
// ans += c.charCodeAt(0) - "A".charCodeAt(0) + 27;
// }
// }
// }
// return ans;
// });
compute(2, async (input) => {
const data = input.parse(f.nl(f.chrs()));
let ans = 0;
for (let i = 0; i < data.length; i += 3) {
const first = Set(data[i]);
const second = Set(data[i + 1]);
const third = Set(data[i + 2]);
const intersection = first.intersect(second).intersect(third);
for (const c of intersection) {
if (c.toLowerCase() === c) {
ans += c.charCodeAt(0) - "a".charCodeAt(0) + 1;
} else {
ans += c.charCodeAt(0) - "A".charCodeAt(0) + 27;
}
}
}
return ans;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment