Solution to Puzzle Node's puzzle #4. Execute with
cat input.txt | ruby process.rb > output.txt
Solution to Puzzle Node's puzzle #4. Execute with
cat input.txt | ruby process.rb > output.txt
class Wall | |
attr_reader :text | |
def initialize(text) | |
@text = text | |
@lasers = [] | |
@text.each_char do |c| | |
@lasers << (c == '|' ? 1 : 0) | |
end | |
end | |
def hits (position, direction, polarity) | |
lasers = direction == :west ? | |
@lasers[0..position].reverse : | |
@lasers[position..-1] | |
lasers = lasers.values_at(* lasers.each_index.select {|e| e.even? ^ (polarity == :odd)}).reduce(0, :+) | |
end | |
end | |
STDIN.read.split("\n\n").each do |problem| | |
lines = problem.split("\n") | |
north_wall = Wall.new(lines[0]) | |
south_wall = Wall.new(lines[2]) | |
position = lines[1].index('X') | |
if north_wall.hits(position, :west, :even) + south_wall.hits(position, :west, :odd) > | |
north_wall.hits(position, :east, :even) + south_wall.hits(position, :east, :odd) then | |
puts 'GO EAST' | |
else | |
puts 'GO WEST' | |
end | |
end |