Created
June 10, 2011 03:01
-
-
Save ngmaloney/1018167 to your computer and use it in GitHub Desktop.
Solution to Puzzlenode.com #4 Robots vs Lasers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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