import fs from "fs"; // Quick implementation of _.chunks function chunks(data: Array, chunkSize: number) { return data.reduce((acc, value, index) => { // Initialize a new chunk if (index % chunkSize === 0) acc.push([]); acc[acc.length - 1].push(value); return acc; }, [] as T[][]); } function parseNumbers(s: string) { return chunks(s.split(""), 3).map((chunk) => Number(chunk.join(""))); } const result = fs .readFileSync("./input.txt") .toString() .split("\n") .slice(0, -1) .map((line) => { const [_cardStr, dataStr] = line.split(": "); const [winning, existing] = dataStr.split("| ").map(parseNumbers); return { winning, existing }; }) .map((data) => data.existing.filter((x) => data.winning.includes(x)).length) .reduce((acc, winCount) => acc + (winCount ? 2 ** (winCount - 1) : 0), 0); console.log({ result });