Re:Coding Challenge for MI5
by basemntunivrse on Sunday September 11, 2016 @12:45PM
(#12474202)
I'm a little disappointed that I was able to solve it so quickly, I thought it was going to be a big multi-stage puzzle where the color values and image dimensions/row boundaries would all be important factors...
Still, my solution (in JS) was a little more verbose than yours...
<img src="puzzle2.png" id="puzzle-image">
<script>
/*
Pink (white) 232,3,138
Blue (black) 58,185,229
*/
window.addEventListener("load", function() {
var image = document.getElementById("puzzle-image"),
canvas = document.createElement("canvas"),
context = canvas.getContext("2d");
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0, image.width, image.height);
// Test 1 - rle
(function() {
var rle = [], prev = null, count = 0;
for (let y = 0; y < image.height; y++) {
for (let x = 0; x < image.width; x++) {
let pd = context.getImageData(x, y, 1, 1).data;
if (prev === null) {
prev = pd[0];
}
if (pd[0] == prev) {
count++;
} else {
rle.push(count);
count = 1;
prev = pd[0];
}
}
}
var r1 = "";
for (let i = 0; i < rle.length; i++) {
r1 += String.fromCharCode(rle[i]);
}
console.log(r1);
var r2 = "";
for (let i = 0; i < r1.length; i += 3) {
r2 += String.fromCharCode(parseInt(r1[i] + r1[i + 1], 16));
}
console.log(r2);
}());
}, false);
</script>
(Where puzzle2.png is the image in black & white)(Edited to fix indentation...)
You could have written this up in a Gist or PasteBin, provided a link and a spoiler message.
My solution:
#!/bin/sh
wget https://www.mi5.gov.uk/sites/default/files/u2/puzzle.png
convert puzzle.png puzzle.xpm # Convert the image to XPM with ImageMagick.
# Here is what the contents of puzzle.xpm looks like:
# http://pastebin.com/0PySSAXx. The spaces represent one color and the dots
# the other. We'll replace the spaces with underscores for readability.
<puzzle.xpm tr ' ' _ \
| tr -d "\n" \
| sed -e 's|","||g; s|^.*\*/"||; s|".*$|| # Remove metadata, C framing.' \
| sed -e 's|_\.|_\n.|g; s|\._|.\n_|g # New line on color change. ' \
| awk '{printf "%c", length($0)}' \
| xxd -r -p
The XPM image file format (https://en.wikipedia.org/wiki/X_PixMap) is great for doing things like this. I find it easier to read and to edit by hand than the Netpbm format family. It seems ImageMagick can't write XPM2, though, which makes extra preprocessing necessary.Huh. So who benefits from you posting the solution here?
Re:Coding Challenge for MI5
by Retr0spectrum on Sunday September 11, 2016 @01:27PM
(#12474443)
It's not like its a particularly complex puzzle. If you don't want spoilers, don't read it.
Can you spoil the latest Bourne film for me as well please?
Re:Coding Challenge for MI5
by cureyourhead on Sunday September 11, 2016 @12:22PM
(#12474080)
Well done!
3. Strip the PBM header lines and remove newlines so that it's just 0's and 1's.
4. Now feed it through this program:
Output is the following: 5. This smells like ASCII, let's decode it with tr -d - | xxd -p -r> Congratulations, you solved the puzzle! Why dont you apply to join our team? mi5.gov.uk/careers
From: https://archive.rebeccablacktech.com/g/thread/52888920#p5289...