Finished
33// convenience to get you started writing code faster.
44//
55
6export const solve = () => {
7 throw new Error('Remove this line and implement the function');
6export const solve = (puzzle) => {
7 // Edge Case: Empty or invalid puzzle format
8 if (!puzzle || typeof puzzle !== 'string') {
9 return null;
10 }
11
12 // Parse the puzzle - handle == correctly
13 const normalizedPuzzle = puzzle.replace(/\s/g, '').replace(/==/g, '=');
14 const parts = normalizedPuzzle.split(/[\+\=]/);
15
16 // Edge Case: Invalid puzzle format - not enough parts
17 if (parts.length < 3) {
18 return null;
19 }
20
21 const addends = parts.slice(0, -1);
22 const result = parts[parts.length - 1];
23 const allWords = [...addends, result];
24
25 // Edge Case: Empty words in puzzle
26 if (allWords.some(word => word.length === 0)) {
27 return null;
28 }
29
30 // Get all unique letters in order of first appearance in puzzle
31 const letters = [];
32 const seen = new Set();
33 for (const char of puzzle) {
34 if (/[A-Z]/.test(char) && !seen.has(char)) {
35 seen.add(char);
36 letters.push(char);
37 }
38 }
39
40 // Edge Case: Too many unique letters (more than 10)
41 if (letters.length > 10) {
42 return null;
43 }
44
45 // Get leading letters (first letter of each word)
46 const leadingLetters = [...new Set(allWords.map(word => word[0]))];
47
48 // Edge Case: No letters to solve
49 if (letters.length === 0) {
50 return null;
51 }
52
53 // Generate all possible digit permutations for the letters
54 const permutations = generatePermutations(Array.from({length: 10}, (_, i) => i), letters.length);
55
56 for (const perm of permutations) {
57 const mapping = {};
58 let valid = true;
59
60 // Create mapping from letters to digits
61 for (let i = 0; i < letters.length; i++) {
62 mapping[letters[i]] = perm[i];
63 }
64
65 // Edge Case: Leading zero constraint
66 if (leadingLetters.some(letter => mapping[letter] === 0)) {
67 continue;
68 }
69
70 // Calculate sum of addends
71 let sum = 0;
72 for (const addend of addends) {
73 let num = 0;
74 for (const char of addend) {
75 num = num * 10 + mapping[char];
76 }
77 sum += num;
78 }
79
80 // Calculate result value
81 let resultValue = 0;
82 for (const char of result) {
83 resultValue = resultValue * 10 + mapping[char];
84 }
85
86 // Edge Case: Valid solution found
87 if (sum === resultValue) {
88 // Return mapping with keys in alphabetical order for consistency
89 const orderedMapping = {};
90 const sortedKeys = Object.keys(mapping).sort();
91 for (const key of sortedKeys) {
92 orderedMapping[key] = mapping[key];
93 }
94 return orderedMapping;
95 }
96 }
97
98 // Edge Case: No solution exists
99 return null;
8100};
101
102// Helper function to generate permutations
103function generatePermutations(arr, size) {
104 const result = [];
105
106 function permute(tempArr, remaining) {
107 if (tempArr.length === size) {
108 result.push([...tempArr]);
109 return;
110 }
111
112 for (let i = 0; i < remaining.length; i++) {
113 const current = remaining[i];
114 const newRemaining = [...remaining.slice(0, i), ...remaining.slice(i + 1)];
115 permute([...tempArr, current], newRemaining);
116 }
117 }
118
119 permute([], arr);
120 return result;
121}
122
123// Handled Edge Cases: Empty or invalid puzzle format, Invalid puzzle format - not enough parts, Empty words in puzzle, Too many unique letters (more than 10), No letters to solve, Leading zero constraint, Valid solution found, No solution exists
Test NameStatus
puzzle with three letters
Pass
solution must have unique value for each letter
Pass
leading zero solution is invalid
Pass
puzzle with four letters
Pass
puzzle with six letters
Pass
puzzle with seven letters
Pass
puzzle with eight letters
Pass
puzzle with ten letters
Pass
puzzle with ten letters and 199 addends
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.