-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2ce17b
commit 35a02d7
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
292: 11 6 16 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |