Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 8, 2021 05:21
Show Gist options
  • Save bluepichu/94ccd0aed5fb8d1eaf0bc5ae0f39076f to your computer and use it in GitHub Desktop.
Save bluepichu/94ccd0aed5fb8d1eaf0bc5ae0f39076f to your computer and use it in GitHub Desktop.
import { List, Set } from "immutable";
import { Advent, int } from "advent";
const { compute, computeCheck } = await Advent({ day: 8 });
compute(async (input) => {
// const data = input.tokens(/,/).map(int);
// const data = input.ints();
const data = input.lines();
let ans = 0;
for (let line of data) {
const [patternStr, rest] = line.split("|");
const patterns = patternStr.trim().split(" ").map(Set);
let onePattern = patterns.find((p) => p.size === 2)!;
let sevenPattern = patterns.find((p) => p.size === 3)!;
let eightPattern = patterns.find((p) => p.size === 7)!;
let fourPattern = patterns.find((p) => p.size === 4)!;
let ninePattern = patterns.find((p) => p.size === 6 && p.intersect(fourPattern).size === 4)!;
let zeroPattern = patterns.find((p) => p.size === 6 && p.intersect(sevenPattern).size === 3 && p != ninePattern)!;
let sixPattern = patterns.find((p) => p.size === 6 && p !== ninePattern && p != zeroPattern)!;
let fivePattern = patterns.find((p) => p.size === 5 && p.intersect(sixPattern).size === 5)!;
let threePattern = patterns.find((p) => p.size === 5 && p.intersect(fourPattern).size === 3 && p != fivePattern)!;
let twoPattern = patterns.find((p) => p.size === 5 && p !== fivePattern && p !== threePattern)!;
let pfin = [
zeroPattern,
onePattern,
twoPattern,
threePattern,
fourPattern,
fivePattern,
sixPattern,
sevenPattern,
eightPattern,
ninePattern,
];
let digits = rest.trim().split(" ").map(Set).map((p) => pfin.findIndex((p2) => p2.equals(p)));
if (digits.some((d) => d === -1)) {
throw new Error();
}
ans += parseInt(digits.join(""));
}
return ans;
}, 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