Last active
August 15, 2017 10:26
-
-
Save LuxXx/848e5796f0ee91174afa49e5190ec19c to your computer and use it in GitHub Desktop.
A script to find solutions to Calculator: The Game
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
// This is a script to find a solution to Calculator: The Game | |
// https://play.google.com/store/apps/details?id=com.sm.calculateme | |
function add(a) { | |
return { | |
apply: function(b) { | |
return a + b; | |
}, | |
info: 'add ' + a | |
} | |
} | |
function multiply(a) { | |
return { | |
apply: function(b) { | |
return a * b; | |
}, | |
info: 'mult ' + a | |
} | |
} | |
function concat(a) { | |
return { | |
apply: function(b) { | |
return parseInt(b.toString() + a.toString()) | |
}, | |
info: 'concat ' + a | |
} | |
} | |
function replace(a, b) { | |
return { | |
apply: function(n) { | |
return parseInt(n.toString().replace(new RegExp(a, 'g'), b)) | |
}, | |
info: 'replace ' + a + ' with ' + b | |
} | |
} | |
let shift = { | |
apply: function(a) { | |
return Math.floor(a / 10) | |
}, | |
info: 'shift' | |
} | |
let square = { | |
apply: function(a) { | |
return a * a; | |
}, | |
info: 'squared' | |
} | |
let flip = multiply(-1); | |
let reverse = { | |
apply: function(a) { | |
return parseInt(a.toString().split('').reverse().join('')) | |
}, | |
info: 'reverse' | |
} | |
// simply bruteforce the solution, the complexity is actions.length^moves, the number of actions is bounded by 6 | |
function bruteforce(start, goal, moves, actions) { | |
while (true) { | |
let number = start; | |
let solution = []; | |
for (let i = 0; i < moves; i++) { | |
let action = actions[Math.floor(actions.length * Math.random())]; | |
number = action.apply(number); | |
solution.push(action.info); | |
if (number === goal) return solution; | |
} | |
} | |
} | |
// Level 62 | |
// add the possible actions here | |
let actions = [concat(10), multiply(4), add(5), reverse]; | |
console.log(bruteforce(0, 102, 4, actions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment