Skip to content

Instantly share code, notes, and snippets.

@wesmaldonado
Created January 17, 2011 04:39
Show Gist options
  • Save wesmaldonado/782499 to your computer and use it in GitHub Desktop.
Save wesmaldonado/782499 to your computer and use it in GitHub Desktop.
What happened to my Sunday evening, a programming story about geocaching (sorta.)
# My sister and brother inlaw got into geocaching
# after I showed them the android app for it... anyhow I was in Olympia for my m
# oms birthday and I showed them that puzzle cache (http://coord.info/GC1JD07)...
# and when my manual math ended up being off by one in my "B" calculation (21-19
# != 3) I uh, got carried away once I got back to seattle. :(
# I only got this carried away though 'cause I'm trying to build a presentation
# on parsers for a conference and I thought this was a reasonable mini language wh
# ere I could show the "quick" hour long solution I came up with versus the actual
# parser one I'll present. I also could've done this faster by just coding this
# like program, but I wanted to try to make it more like a programming language...
# and if you took the real example code straight from what they gave you, it woul
# dn't work because some of the variables would've still been undefined, so I nee
# ded to reorder them. :P I'm a dork.
# Wes
# START THE CODES
#! ruby
clue = "humptydumpty"
forumulas = %q{
@a = send(:"1st letter") * 0
@b = send(:"2nd letter") - 19
@c = @a * @b
@d = send(:"3rd letter") - 4
@e = send(:"4th letter") - send(:"9th letter")
@f = send(:"12th letter") - send(:"5th letter")
@j = send(:"12th letter") - (@d + @d)
@h = send(:"2nd letter") - send(:"11th letter")
@g = @j - @h
@i = send(:"7th letter") + 1
@report = %( N 47 #{@a}#{@b}.#{@c}#{@d}#{@e} \n W 122 #{@f}#{@g} #{@h}#{@i}#{@j} \n)
}
PuzzleCacheLang.solve clue, forumulas
# Outputs....
#
# [wesmaldonado@wombatbadger:~/workspace/puzzle-cache-lang] $ ruby pcl.rb
# N 47 02.093
# W 122 56 157
# ---- http://about.me/wesmaldonado ----
# twitter ::  http://twitter.com/wes
class PuzzleCacheLang
attr_accessor :clue
attr_reader :letter_position
attr_accessor :solver
def initialize(clue)
self.clue = clue
@letter_position = {}
('a'..'z').to_a.each_with_index.map { |x, i| @letter_position[x] = i+1 }
@clue_to_alpha_value = clue.split("").map { |l| @letter_position[l] }
self
end
def method_missing(meth, *args)
if meth.to_s =~ /^(\d+)\w{2}\s+letter/
@clue_to_alpha_value[$1.to_i - 1]
end
end
def self.woo
self.solve("humptydumpty",%q{ @i = send(:"7th letter") + 1 })
end
def self.solve clue, formulas
solver = PuzzleCacheLang.new(clue)
formulas.split("\n").each do |f|
next if f.length == 0
exp = "eval(' #{f} ')"
begin
solver.instance_eval exp
rescue => e
self.report(solver, exp, e)
end
end
puts solver.instance_variable_get :@report
solver
end
def self.report(s, exp, ex)
puts "exp was: #{exp}"
puts "Error was #{ex.message}"
s.instance_variables.each do |iv|
puts "#{iv} => " + s.instance_variable_get(iv).inspect
end
exit
end
end
clue = "humptydumpty"
forumulas = %q{
@a = send(:"1st letter") * 0
@b = send(:"2nd letter") - 19
@c = @a * @b
@d = send(:"3rd letter") - 4
@e = send(:"4th letter") - send(:"9th letter")
@f = send(:"12th letter") - send(:"5th letter")
@j = send(:"12th letter") - (@d + @d)
@h = send(:"2nd letter") - send(:"11th letter")
@g = @j - @h
@i = send(:"7th letter") + 1
@report = %( N 47 #{@a}#{@b}.#{@c}#{@d}#{@e} \n W 122 #{@f}#{@g} #{@h}#{@i}#{@j} \n)
}
PuzzleCacheLang.solve clue, forumulas
#Outputs....
#[wesmaldonado@wombatbadger:~/workspace/puzzle-cache-lang] $ ruby pcl.rb
# N 47 02.093
# W 122 56 157
@wesmaldonado
Copy link
Author

The thing that makes this a possible language/parser candidate is that the puzzle language is moderately consistant. Letters get translated to the position in the alphabets, elements use short letter abbreviations to their atomic numbers or weights, etc. And there are phrases like "2nd letter from each X" things like that. Simple transforms overall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment