const sample = await Deno.readTextFile("sample.txt"); const input = await Deno.readTextFile("input.txt"); const solvePart1 = (data: string): number => data.split("\n").filter(Boolean).map((line) => line.split(/:\ +/)[1]?.split(/\ +\|\ +/).map((parts) => parts.split(/\ +/).map(Number) ) ) .map(([winning, yours]) => yours.filter((n) => winning.includes(n))) .map((matches) => matches.reduce((a, _, i) => i === 0 ? 1 : a * 2, 0)) .reduce((s, v) => s + v, 0); console.log("Sample:", solvePart1(sample)); // 13 console.log("Input", solvePart1(input)); // 22488 const solvePart2 = (data: string): number => { const x = data .split("\n") .filter(Boolean) .map((line) => line .split(/:\ +/)[1] ?.split(/\ +\|\ +/) .map((parts) => parts.split(/\ +/).map(Number)) ) .map(([winning, yours]) => yours.filter((n) => winning.includes(n))) .map((matches, index) => [index + 1, matches.length]) .reduce((r, [i, l]) => { r[i] = r[i] || 1 for (let j = i + 1; j < i + l + 1; j += 1) { r[j] = (r[j] ?? 1) + (1 * (r[i] ?? 1)); } return r; }, { "1": 1 } as Record); return Object.values(x).reduce((s, v) => s + v); }; console.log("Sample2:", solvePart2(sample)); // 30 console.log("Input", solvePart2(input)); // 7013204