-
-
Save tkihira/e27ba639de5be545f79b8c3decead68d to your computer and use it in GitHub Desktop.
Solution of 2023 puzzle
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
10*9/((8-7)/(6*5/(4/3)))-2*1 | |
10*9/((8-7)/(6*5/(4/3)))-2/1 | |
(10*9/((8-7)/(6*5/(4/3)))-2)*1 | |
(10*9/((8-7)/(6*5/(4/3)))-2)/1 | |
10*9/((8-7)/(6*5/4*3))-2*1 | |
10*9/((8-7)/(6*5/4*3))-2/1 | |
(10*9/((8-7)/(6*5/4*3))-2)*1 | |
(10*9/((8-7)/(6*5/4*3))-2)/1 | |
10*9/((8-7)/(6*5/4)/3)-2*1 | |
10*9/((8-7)/(6*5/4)/3)-2/1 | |
(10*9/((8-7)/(6*5/4)/3)-2)*1 | |
(10*9/((8-7)/(6*5/4)/3)-2)/1 | |
10*9/((8-7)/(6*5/4))*3-2*1 | |
10*9/((8-7)/(6*5/4))*3-2/1 | |
(10*9/((8-7)/(6*5/4))*3-2)*1 | |
(10*9/((8-7)/(6*5/4))*3-2)/1 | |
10*9/((8-7)/(6*5)*4/3)-2*1 | |
10*9/((8-7)/(6*5)*4/3)-2/1 | |
(10*9/((8-7)/(6*5)*4/3)-2)*1 | |
(10*9/((8-7)/(6*5)*4/3)-2)/1 | |
10*9/((8-7)/(6*5)*4)*3-2*1 | |
10*9/((8-7)/(6*5)*4)*3-2/1 | |
(10*9/((8-7)/(6*5)*4)*3-2)*1 | |
(10*9/((8-7)/(6*5)*4)*3-2)/1 | |
10*9/((8-7)/(6*5))/(4/3)-2*1 | |
10*9/((8-7)/(6*5))/(4/3)-2/1 | |
(10*9/((8-7)/(6*5))/(4/3)-2)*1 | |
(10*9/((8-7)/(6*5))/(4/3)-2)/1 | |
10*9/((8-7)/(6*5))/4*3-2*1 | |
10*9/((8-7)/(6*5))/4*3-2/1 | |
(10*9/((8-7)/(6*5))/4*3-2)*1 | |
(10*9/((8-7)/(6*5))/4*3-2)/1 | |
10*9/((8-7)/6/(5/(4/3)))-2*1 | |
10*9/((8-7)/6/(5/(4/3)))-2/1 | |
(10*9/((8-7)/6/(5/(4/3)))-2)*1 | |
(10*9/((8-7)/6/(5/(4/3)))-2)/1 | |
10*9/((8-7)/6/(5/4*3))-2*1 | |
10*9/((8-7)/6/(5/4*3))-2/1 | |
(10*9/((8-7)/6/(5/4*3))-2)*1 | |
(10*9/((8-7)/6/(5/4*3))-2)/1 | |
10*9/((8-7)/6/(5/4)/3)-2*1 | |
10*9/((8-7)/6/(5/4)/3)-2/1 | |
(10*9/((8-7)/6/(5/4)/3)-2)*1 | |
(10*9/((8-7)/6/(5/4)/3)-2)/1 | |
10*9/((8-7)/6/(5/4))*3-2*1 | |
10*9/((8-7)/6/(5/4))*3-2/1 | |
(10*9/((8-7)/6/(5/4))*3-2)*1 | |
(10*9/((8-7)/6/(5/4))*3-2)/1 | |
10*9/((8-7)/6/5*4/3)-2*1 | |
10*9/((8-7)/6/5*4/3)-2/1 | |
(10*9/((8-7)/6/5*4/3)-2)*1 | |
(10*9/((8-7)/6/5*4/3)-2)/1 | |
10*9/((8-7)/6/5*4)*3-2*1 | |
10*9/((8-7)/6/5*4)*3-2/1 | |
(10*9/((8-7)/6/5*4)*3-2)*1 | |
(10*9/((8-7)/6/5*4)*3-2)/1 | |
10*9/((8-7)/6/5)/(4/3)-2*1 | |
10*9/((8-7)/6/5)/(4/3)-2/1 | |
(10*9/((8-7)/6/5)/(4/3)-2)*1 | |
(10*9/((8-7)/6/5)/(4/3)-2)/1 | |
10*9/((8-7)/6/5)/4*3-2*1 | |
10*9/((8-7)/6/5)/4*3-2/1 | |
(10*9/((8-7)/6/5)/4*3-2)*1 | |
(10*9/((8-7)/6/5)/4*3-2)/1 | |
10*9/((8-7)/6)*5/(4/3)-2*1 | |
10*9/((8-7)/6)*5/(4/3)-2/1 | |
(10*9/((8-7)/6)*5/(4/3)-2)*1 | |
(10*9/((8-7)/6)*5/(4/3)-2)/1 | |
10*9/((8-7)/6)*5/4*3-2*1 | |
10*9/((8-7)/6)*5/4*3-2/1 | |
(10*9/((8-7)/6)*5/4*3-2)*1 | |
(10*9/((8-7)/6)*5/4*3-2)/1 | |
10*9/(8-7)*6*5/(4/3)-2*1 | |
10*9/(8-7)*6*5/(4/3)-2/1 | |
(10*9/(8-7)*6*5/(4/3)-2)*1 | |
(10*9/(8-7)*6*5/(4/3)-2)/1 | |
10*9/(8-7)*6*5/4*3-2*1 | |
10*9/(8-7)*6*5/4*3-2/1 | |
(10*9/(8-7)*6*5/4*3-2)*1 | |
(10*9/(8-7)*6*5/4*3-2)/1 | |
((10+(9/8+7)*6*5)*4-3)*2-1 |
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
const prettyPrintLog = {}; | |
const prettyPrint = (expr) => { | |
const stack = []; | |
for (const c of expr) { | |
switch (c) { | |
case '+': { | |
const r = stack.pop(); | |
const l = stack.pop(); | |
stack.push({ str: l.str + c + r.str, priority: 3 }); | |
break; | |
} | |
case '-': { | |
const r = stack.pop(); | |
const l = stack.pop(); | |
if (r.priority < 3) { | |
stack.push({ str: l.str + c + r.str, priority: 3 }); | |
} else { | |
stack.push({ str: l.str + c + '(' + r.str + ')', priority: 3 }); | |
} | |
break; | |
} | |
case '*': { | |
const r = stack.pop(); | |
const l = stack.pop(); | |
const rStr = r.priority === 3 ? `(${r.str})` : r.str; | |
const lStr = l.priority === 3 ? `(${l.str})` : l.str; | |
stack.push({ str: lStr + c + rStr, priority: 2 }); | |
break; | |
} | |
case '/': { | |
const r = stack.pop(); | |
const l = stack.pop(); | |
const rStr = r.priority >= 2 ? `(${r.str})` : r.str; | |
const lStr = l.priority === 3 ? `(${l.str})` : l.str; | |
stack.push({ str: lStr + c + rStr, priority: 2 }); | |
break; | |
} | |
default: | |
stack.push({ str: c, priority: 1 }); | |
} | |
} | |
const output = stack.pop().str; | |
if (!prettyPrintLog[output]) { | |
prettyPrintLog[output] = true; | |
console.log(output); | |
} | |
}; | |
const solve = (expr) => { | |
const stack = []; | |
for (const item of expr) { | |
switch (item) { | |
case '+': stack.push(stack.pop() + stack.pop()); break; | |
case '-': stack.push(-stack.pop() + stack.pop()); break; | |
case '*': stack.push(stack.pop() * stack.pop()); break; | |
case '/': stack.push(1 / stack.pop() * stack.pop()); break; | |
default: stack.push(Number(item)); break; | |
} | |
} | |
const result = stack.pop(); | |
if (Math.abs(result - 2023) < 0.001) { | |
prettyPrint(expr); | |
} | |
}; | |
const traverse = (expr, number, opes, nums, eightDepth) => { | |
const opCharList = [..."n+-*/"]; | |
if (number === 0 && nums - opes === 1) { | |
solve(expr) | |
} | |
for (const op of opCharList) { | |
if (op === 'n') { | |
if (number > 0) { | |
expr.push(number); | |
if (traverse(expr, number - 1, opes, nums + 1, number === 8 ? 0 : eightDepth < 0 ? eightDepth : eightDepth + 1)) { | |
return true; | |
} | |
expr.pop(); | |
} | |
} else { | |
if (nums - opes >= 2) { | |
if (eightDepth === 0 && op !== '/') { | |
continue; | |
} | |
expr.push(op); | |
if (traverse(expr, number, opes + 1, nums, eightDepth - 1)) { | |
return true; | |
} | |
expr.pop(); | |
} | |
} | |
} | |
}; | |
traverse([], 10, 0, 0, -1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment