Created
December 10, 2021 05:11
-
-
Save bluepichu/340a7932b730a4c9f3d47240e8c0d187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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