Skip to content

Instantly share code, notes, and snippets.

@tkihira
Created January 3, 2023 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkihira/e27ba639de5be545f79b8c3decead68d to your computer and use it in GitHub Desktop.
Save tkihira/e27ba639de5be545f79b8c3decead68d to your computer and use it in GitHub Desktop.
Solution of 2023 puzzle
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/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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
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