From 3577383945cbdb2190269a430ebd5b0173f1c3dc Mon Sep 17 00:00:00 2001 From: Cool-Katt Date: Wed, 23 Oct 2024 15:44:59 +0300 Subject: [PATCH] Add proof solution --- .../killer-sudoku-helper/.meta/proof.ci.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/exercises/practice/killer-sudoku-helper/.meta/proof.ci.js b/exercises/practice/killer-sudoku-helper/.meta/proof.ci.js index e69de29bb2..570c87c060 100644 --- a/exercises/practice/killer-sudoku-helper/.meta/proof.ci.js +++ b/exercises/practice/killer-sudoku-helper/.meta/proof.ci.js @@ -0,0 +1,28 @@ +export const combinations = (cage) => { + const { sum, size, exclude } = cage; + const result = []; + const digits = [...Array(10).keys()] + .slice(1) + .filter((d) => !exclude.includes(d)); + + function findCombinations(remainingSum, index, combination) { + if (remainingSum === 0 && combination.length === size) { + result.push(combination); + return; + } + + for (let i = index; i < digits.length; i++) { + const digit = digits[i]; + if (digit > remainingSum) { + break; + } + if (combination.includes(digit)) { + continue; + } + findCombinations(remainingSum - digit, i + 1, [...combination, digit]); + } + } + + findCombinations(sum, 0, []); + return result; +};