From 35a02d77cdf0888c8cf4e3f0217c9011e60a18c7 Mon Sep 17 00:00:00 2001
From: Wiktoria Van Harneveldt <w.dev+git@mailbox.org>
Date: Sat, 7 Dec 2024 17:50:16 +0100
Subject: [PATCH] wip: attempting d7 p1

---
 inputs/7.txt   |   1 +
 src/7.ts       | 115 +++++++++++++++++++++++++++++++++++++++++++++++++
 test/7.test.ts |  20 +++++++++
 3 files changed, 136 insertions(+)
 create mode 100644 inputs/7.txt
 create mode 100644 src/7.ts
 create mode 100644 test/7.test.ts

diff --git a/inputs/7.txt b/inputs/7.txt
new file mode 100644
index 0000000..1b8f66d
--- /dev/null
+++ b/inputs/7.txt
@@ -0,0 +1 @@
+292: 11 6 16 20
\ No newline at end of file
diff --git a/src/7.ts b/src/7.ts
new file mode 100644
index 0000000..87ddb23
--- /dev/null
+++ b/src/7.ts
@@ -0,0 +1,115 @@
+type Operator = "+" | "*";
+
+function solution(input: string): { part1: string; part2: string } {
+  let result1 = 0;
+  let result2 = 0;
+
+  const lines = input.split("\n").map((line) => {
+    const [result, nums] = line.split(": ");
+    return {
+      result: parseInt(result),
+      nums: nums.split(" ").map((num) => parseInt(num)),
+    };
+  });
+
+  const operators: Operator[] = ["*", "+"];
+  const correctResults: number[] = [];
+
+  lines.forEach((line) => {
+    const { result, nums } = line;
+    const usedOperators = new Set<string>();
+
+    let inputOperations: string = "";
+
+    function getOperator(operatorIndex: number, loop: number = 0): Operator {
+      let index = operatorIndex;
+      let operator = operators[index];
+      if (
+        operator === "*" &&
+        usedOperators.size === Math.pow(2, nums.length - 1) / 2 &&
+        loop === 0
+      ) {
+        console.log("4 times", ...usedOperators);
+        index = (operatorIndex + 1) % 2;
+        operator = operators[index];
+      }
+      const createdOperations = `${inputOperations}${operator}`;
+      if (createdOperations === "++") {
+        console.log("createdOperations", createdOperations);
+      }
+      if (usedOperators.has(createdOperations)) {
+        index = (operatorIndex + 1) % 2;
+        return getOperator(index, loop + 1);
+      } else {
+        inputOperations = createdOperations;
+        return operator;
+      }
+    }
+
+    function calculate(alpha: number, beta: number, operator: Operator) {
+      return eval(`${alpha} ${operator} ${beta}`);
+    }
+
+    let prev = nums[0];
+    let p1 = 1;
+    const lenghtOfOperations = nums.length - 1;
+    const variantsOfOperations = Math.pow(2, lenghtOfOperations);
+    console.log("-----");
+
+    while (p1 < nums.length) {
+      if (variantsOfOperations === usedOperators.size) {
+        inputOperations = "";
+        break;
+      }
+      let operator = "*";
+      try {
+        operator = getOperator(0);
+      } catch (error) {
+        break;
+      }
+
+      const num = nums[p1];
+      prev = eval(`${prev} ${operator} ${num}`);
+
+      if (prev > result) {
+        prev = nums[0];
+        p1 = 1;
+        usedOperators.add(inputOperations);
+        inputOperations = "";
+        continue;
+      }
+
+      if (prev === result && inputOperations.length === lenghtOfOperations) {
+        correctResults.push(result);
+        inputOperations = "";
+        break;
+      }
+
+      if (p1 === nums.length - 1) {
+        prev = nums[0];
+        p1 = 1;
+        usedOperators.add(inputOperations);
+        inputOperations = "";
+      }
+
+      if (inputOperations.length === lenghtOfOperations) {
+        inputOperations = "";
+        prev = nums[0];
+        p1 = 1;
+        usedOperators.add(inputOperations);
+      }
+
+      p1++;
+    }
+  });
+
+  console.log(correctResults);
+  result1 = correctResults.reduce((acc, curr) => acc + curr, 0);
+
+  return {
+    part1: result1.toString(),
+    part2: result2.toString(),
+  };
+}
+
+export default solution;
diff --git a/test/7.test.ts b/test/7.test.ts
new file mode 100644
index 0000000..e0e5ee8
--- /dev/null
+++ b/test/7.test.ts
@@ -0,0 +1,20 @@
+import { expect, test, describe } from "bun:test";
+import solution from "../src/7";
+
+const EXAMPLE_1 = `292: 11 6 16 20`;
+const RESULT_1 = "3749";
+
+const EXAMPLE_2 = EXAMPLE_1;
+const RESULT_2 = "0";
+
+describe("Day 7", () => {
+  test("Part 1", () => {
+    const result = solution(EXAMPLE_1);
+    expect(result.part1).toBe(RESULT_1);
+  });
+
+  test("Part 2", () => {
+    const result = solution(EXAMPLE_2);
+    expect(result.part2).toBe(RESULT_2);
+  });
+});