Skip to content

Instantly share code, notes, and snippets.

@kezzyhko
Created November 5, 2020 17:27
Show Gist options
  • Save kezzyhko/4ca4e7a20263600dcea65c6f97bc103a to your computer and use it in GitHub Desktop.
Save kezzyhko/4ca4e7a20263600dcea65c6f97bc103a to your computer and use it in GitHub Desktop.
A bruteforce solver for Calculator: The Game
<?php
echo '<pre>';
define('start_num', 100);
define('end_num', 101);
define('moves', 5);
define('functions', explode(' ', '0 x2 2=>10 0=>1 Reverse'));
function check($current_num, $moves_left=moves, $history='') {
if ($current_num == end_num) {
echo $history;
echo "\n";
}
if ($moves_left>0) {
foreach(functions as $func) {
if (strpos($func, '=>')!==false) {
$nums = explode('=>', $func);
$new_num = str_replace($nums[0], $nums[1], $current_num);
} else {
$new_num = $current_num;
switch ($func) {
case 'Reverse':
$new_num = ($new_num <=> 0) * strrev($new_num);
break;
case '+/-':
$new_num = -$new_num;
break;
case '<<':
$new_num = substr($new_num, 0, -1);
break;
default:
switch ($func[0]) {
case '+':
case '-':
$new_num += $func;
break;
case 'x':
$new_num *= substr($func, 1);
break;
case '/':
$new_num /= substr($func, 1);
break;
default:
$new_num .= $func;
break;
}
}
}
check(
$new_num,
$moves_left-1,
$history . ' ' . $func
);
}
}
}
check(start_num);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment