Last active
January 16, 2020 05:55
-
-
Save jv-k/2f2635af17f65e14334fd71f3ae26c6d to your computer and use it in GitHub Desktop.
My solution to this Guardian puzzle (my solution was highlighted π): https://www.theguardian.com/science/2020/jan/13/did-you-solve-it-the-poco-poco-puzzle#comment-137362390
This file contains hidden or 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
// https://www.theguardian.com/science/2020/jan/13/did-you-solve-it-the-poco-poco-puzzle#comment-137362390 | |
// I did it with code! | |
var P, O, C; | |
var M, U, _C, H, _O; | |
var greatSuccess = false; | |
var result; | |
// Just some colours for pretty printing results | |
const col = { | |
reset: '\033[0m', | |
//text color | |
black: '\033[30m', | |
red: '\033[31m', | |
green: '\033[32m', | |
yellow: '\033[33m', | |
blue: '\033[34m', | |
magenta: '\033[35m', | |
cyan: '\033[36m', | |
white: '\033[37m', | |
//background color | |
blackBg: '\033[40m', | |
redBg: '\033[41m', | |
greenBg: '\033[42m', | |
yellowBg: '\033[43m', | |
blueBg: '\033[44m', | |
magentaBg: '\033[45m', | |
cyanBg: '\033[46m', | |
whiteBg: '\033[47m' | |
} | |
var cO = col.red | |
cC = col.yellow; | |
console.log( "\n " + "P " + cO + "O " + cC + "C " + cO + "O" + col.reset); | |
console.log(" " + "M " + "U " + cC + "C " + col.reset + "H " + cO + "O" + col.reset); | |
console.log("______________"); | |
for (P = 0; P < 10; P++) { | |
for (O = 0; O < 10; O++) { | |
for (C = 0; C < 10; C++) { | |
// We're looking for: | |
// 15 * POCO | |
// = MUCHO | |
// | |
// where each letter stands for a unique digit | |
greatSuccess = false; | |
result = "" + (15 * ("" + P + O + C + O)); | |
M = result.charAt(0); M = M ? M : 0; | |
U = result.charAt(1); U = U ? U : 0; | |
C_ = result.charAt(2); // C in results | |
H = result.charAt(3); H = H ? H : 0; | |
O_ = result.charAt(4); // O in results | |
greatSuccess = ( | |
result.length == 5 | |
&& C_ == C | |
&& O_ == O | |
&& noDuplicatedChars("" + P + O + C + M + U + H) | |
); | |
if (greatSuccess) { | |
console.log( "\n15 x " + P + " " + cO + O + " " + cC + C + " " + cO + O + col.reset); | |
console.log(" = " + M + " " + U + " " + cC + C_ + col.reset + " " + H + " " + cO + O_ + col.reset); | |
} | |
} | |
} | |
} | |
function noDuplicatedChars(str) { | |
const chars = new Set(); | |
for (let c of str) { | |
if (chars.has(c)) return false; | |
chars.add(c); | |
} | |
return true; | |
} | |
console.log(col.white + col.greenBg+"\ndone.\n"); | |
// Result: | |
// 15 x 4595 = 68925 |
This file contains hidden or 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
# Python implementation | |
# Credit: Dick Pountain #comment-137361347 | |
for p in range(0, 10): | |
for o in range(0, 10): | |
for c in range(0, 10): | |
if p != o and p != c and c != o: | |
z = (p * 1000 + o * 100 + c * 10 + o) * 15 | |
for m in range(0, 10): | |
for u in range(0, 10): | |
for h in range(0, 10): | |
x = m * 10000 + u * 1000 + c * 100 + h * 10 + o | |
if z == x and m != u and m != h and h != u and m != c and h != c and u != c and h != o and u != o and u != p and m != p and h != p: | |
print(p, o, c, o, ' ', m, u, c, h, o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment