Skip to content

Instantly share code, notes, and snippets.

@ngmaloney
Created June 10, 2011 03:01
Show Gist options
  • Save ngmaloney/1018167 to your computer and use it in GitHub Desktop.
Save ngmaloney/1018167 to your computer and use it in GitHub Desktop.
Solution to Puzzlenode.com #4 Robots vs Lasers
class RobotGuidanceSystem
attr_accessor :schematics
def initialize(input_file)
self.schematics = []
f = File.open(input_file) if File.exists? input_file
parse_schematics f.read if f
end
def run
schematics.each do |s|
puts run_conveyer s
end
end
def run_conveyer conveyer
if(get_damage(conveyer.map{|x| x.reverse}) <= get_damage(conveyer))
return "GO WEST"
end
return "GO EAST"
end
def get_damage conveyer
damage = 0
clicks = 0
start_position = conveyer[1].index 'X'
conveyer[1].slice(start_position,conveyer[1].count).each_with_index do |p,idx|
pos = idx + start_position
if conveyer[0][pos] == '|' and clicks.even?
damage += 1
elsif conveyer[2][pos] == '|' and clicks.odd?
damage += 1
end
clicks += 1
end
return damage
end
def parse_schematics data
i = 1
conveyer = []
data.each_line do |l|
row = l.strip.split("")
conveyer.push row unless row.empty?
if conveyer.count == 3
self.schematics.push conveyer
conveyer = []
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment