summaryrefslogtreecommitdiff
path: root/day-4/script.ts
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-12-04 12:52:11 +0300
committereug-vs <eugene@eug-vs.xyz>2023-12-04 12:52:11 +0300
commit78f94d5bde56dc30fbb845ebab56a7f0ebda8e14 (patch)
treebb664cec3e86f9c167bab8bbd299bda70bd6aa23 /day-4/script.ts
parente9d6f8cc5d7e6959dfe60cab2ca859b3e8fc7788 (diff)
downloadaoc-2023-78f94d5bde56dc30fbb845ebab56a7f0ebda8e14.tar.gz
feat(day-4): add part 1 solution
Diffstat (limited to 'day-4/script.ts')
-rw-r--r--day-4/script.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/day-4/script.ts b/day-4/script.ts
new file mode 100644
index 0000000..53488b5
--- /dev/null
+++ b/day-4/script.ts
@@ -0,0 +1,30 @@
+import fs from "fs";
+
+// Quick implementation of _.chunks
+function chunks<T>(data: Array<T>, 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 });