Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 10, 2021 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluepichu/340a7932b730a4c9f3d47240e8c0d187 to your computer and use it in GitHub Desktop.
Save bluepichu/340a7932b730a4c9f3d47240e8c0d187 to your computer and use it in GitHub Desktop.
import { Advent } from "advent";
const { compute, computeCheck } = await Advent({ day: 10 });
compute(async (input) => {
// const data = input.tokens(/,/).map(int);
// const data = input.ints();
const data = input.lines();
const left = ["(", "[", "{", "<"];
const right = [")", "]", "}", ">"];
const score: { [key: string]: number } = {
")": 3,
"]": 57,
"}": 1197,
">": 25137
}
// let ans = 0;
let scores = [];
for (let line of data) {
let ok = true;
let stack = [];
for (let token of line) {
if (left.indexOf(token) >= 0) {
stack.push(token);
} else {
let last = stack.pop()!;
if (right.indexOf(token) !== left.indexOf(last)) {
ok = false;
break;
}
}
}
if (ok) {
console.log(stack);
let score = 0;
while (stack.length > 0) {
score *= 5;
let posScore = left.indexOf(stack.pop()!);
console.log(posScore);
score += posScore + 1;
}
console.log(score);
scores.push(score);
}
}
scores.sort((a, b) => a - b);
return scores[Math.floor(scores.length / 2)];
}, 2);
// computeCheck(async function* (input) {
// yield 0;
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment