challange 18

I have been wanting to write a webapp using ruby on rails and so I have sporadically been exploring the framework. The last time I did it became quite evident that my ruby skills are getting rusty. It seemed like a perfect excuse to get back to the Pytho- I mean, Ruby Challange.

Challenge 18 just shows two pictures of a goose side by side. The pictures are the same but one is brighter than the other. The title asks “can you tell the difference?” and the page source says
"-- it is more obvious that what you might think --"
And it is indeed obvious, don’t think too hard about it and change the url. After a few tries I found brightness.html which is an identical page with one difference, the page source says
"-- maybe consider deltas.gz --"

deltas.gz contains a textfile with two columns of of 18 two digit hex values. They start out the same but they quickly start to change. It seems pretty obvious that the differences need to be found; my suspicion was backed up by a google search of ‘deltas python’ with the difflib page being top ranked.

Looking around a bit I was unable to find a standard library in ruby for finding the differences in two files or strings the way gnu diff or pythons difflib does. This makes sense when you consider that most of the time ruby is running on a unix/linux system with perfectly capable binary tools for such work.

I did find several gems that provide similar functionality, but nothing that would work quit right. HTMLDiff was the closest, its output is formated in html which was a minor annoyance. However the bigger problem I had was that it frequently falsely classified lines as new and old rather than just marking the lines around them as changed. This resulted in unchanged lines being sent to both the new and the old categories rather than the unchanged category. After banging my head against various gems I finally gave up and used gnu diff. The output is three png files.

#!/usr/bin/ruby
# Challenge 18
require 'zlib'

Zlib::GzipReader.open('deltas.gz') {|gzFile|
# Found lots of hexidecimal values in two columns

        column, output = ['', ''], ['', '', '']
        splitLines = gzFile.read.split("\n")
# Split the two columns apart row by row 
        splitLines.each { |line|
                if line[0, 53].rstrip != ""
                        column[0] += (line[0, 53].rstrip + "\n")
                end
                if line[56, 53].rstrip != ""
                        column[1] += (line[56, 53].rstrip + "\n")
                end
                
# Since I am using gnu diff I need to write them to files 
                column.length.times {|i|
                        f = File.new("column" + i.to_s, "w")
                        f.write(column[i])
                        f.close
                }
        }
        system("diff column0 column1 --old-line-format='- %L' --new-line-format='+ %L'  \
               --unchanged-line-format='%L' >output")
        
# Separate the output of diff into three strings        
        differences = File.open("output", "r")
        differences.read.split("\n").each { |line|
                if line[0, 2] == "+ "
                        output[0] += (line.gsub("+ ", "") + ' ') 
                elsif line[0, 2] == "- " 
                        output[1] += (line.gsub("- ", "") + ' ') 
                else
                        output[2] += (line + ' ')
                end
        }
        differences.close
# Finally, change the hex values into their equivalent characters and write them to file.
        output.length.times { |i|
            chrString = ''
                output[i].split().each {|hexVal|
                        chrString += hexVal.hex.chr
                        f = File.new("deltas18_" + i.to_s + ".png", "w")
                        f.write(chrString)
                        f.close()
                }       
        }
}

With a new url, login and password we arrive at challange 19: “Please!”

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s