challenge 14

walk around

This challenge presents you with a picture of a roll and below it a what appears to be a bar code. The ‘bar code’ image is named ‘wire.png’ and though it is displayed 100×100 it is actually 10000×1. It seems obvious that this wire.png’s pixels need to be rearranged into a 100×100 image. If you try the simplest method, just dumping the pixel data from wire into a new image one line at a time, it says ‘bit’ in red letters. This is not the solution, ‘bit.html’ will tell you that ‘you took the wrong curve.’.

To find the real solution you need to look in the page source, there you will find this;

<!-- remember: 100*100 = (100+99+99+98) + (...  -->

Taking a cue from the direction of the twist in the roll and the title of the page, we should arrive at something like this;

require 'RMagick'
include Magick
xy, mov, p, d = 99, -1, 0, 0, 100).write("./ruby14.jpg")
wi ="./wire.png", "./ruby14.jpg")
while xy >= 49
  if d == 0
    bgn, nd, fxd, mov, up = 99-xy, xy, 99-xy, mov*(-1), true
  elsif d == 1
    bgn, nd, fxd, xy, up = 100-xy, xy, xy, xy-1, false
  elsif d == 2
    bgn, nd, fxd, mov, up = xy, 98-xy, xy+1, mov*(-1), true
    bgn, nd, fxd, d, up = xy, 99-xy, 98-xy, -1, false
  d += 1
  bgn.step(nd, mov){|vpx|
    pixel = wi[0].export_pixels(p, 0, 1, 1, "RGB")
    if up
      wi[1].import_pixels(vpx, fxd, 1, 1, "RGB", pixel)
      wi[1].import_pixels(fxd, vpx, 1, 1, "RGB", pixel)
    end; p += 1 }

It is kind of confusing with all the variables, and I know that there must be a more elegant solution, but it is all I could come up with. I kept thinking that there must be a way to export the the pixel data from ‘wire .png’ into an array properly ordered for direct import to the new image, but I couldn’t get it to work that way. I eventually gave up, this just copied the pixels one at a time to the locations I needed them in the new image.


I was unhappy with the above solution due to its complexity. I had a hell of a time getting all the numbers right. I knew that there was a simpler way to do this, then it hit me last night – arrays. I used an x array and a y array to hold all the possible values of x and y, then shorten and reverse them when appropriate. I think the result is much cleaner, easier to understand and uses fewer variables;

require 'RMagick'
include Magick 
w = 0, 100).write("./ruby14.jpg")
wi ="./wire.png", "./ruby14.jpg")
xa = (0..99).to_a
ya = xa.dup
while xa.length >= 1
    for n in xa
      pixel = wi[0].export_pixels(w, 0, 1, 1, "RGB")
      wi[1].import_pixels(n, ya[0], 1, 1, "RGB", pixel)
      w += 1
    for n in ya
      pixel = wi[0].export_pixels(w, 0, 1, 1, "RGB")
      wi[1].import_pixels(xa[-1], n, 1, 1, "RGB", pixel)
      w += 1

Next: python challenge 15; whom?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s