Snippets

Mehdi Dana GCHQ puzzle 1

Created by Mehdi Dana
<?php

// http://www.bbc.co.uk/programmes/articles/5wkxjTtqRvq8Cyrrjxtk7tc/puzzle-for-today

$n = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$s = ['', '+', '-'];
$co = [];

for ($i1 = 0; $i1 < 3; $i1++) {
    for ($i2 = 0; $i2 < 3; $i2++) {
        for ($i3 = 0; $i3 < 3; $i3++) {
            for ($i4 = 0; $i4 < 3; $i4++) {
                for ($i5 = 0; $i5 < 3; $i5++) {
                    for ($i6 = 0; $i6 < 3; $i6++) {
                        for ($i7 = 0; $i7 < 3; $i7++) {
                            for ($i8 = 0; $i8 < 3; $i8++) {
                                $co[] = [$s[$i1], $s[$i2], $s[$i3], $s[$i4], $s[$i5], $s[$i6], $s[$i7], $s[$i8]];
                            }
                        }
                    }
                }
            }
        }
    }
}

foreach ($co as $c) {
    $t = $n[0] . $c[0] . $n[1] . $c[1] . $n[2] . $c[2] . $n[3] . $c[3] . $n[4] . $c[4] . $n[5] . $c[5] . $n[6] . $c[6] . $n[7] . $c[7] . $n[8];

    $to = eval('return ' . $t . ';');
    if ($to == 100) {
        echo $t . '=' . $to . PHP_EOL;
    }
}

Comments (0)