[2016][ruby][1] refactoring

profile
Alpha Chen 8 years ago
parent b77d565b97
commit ddbface9e5

@ -1,39 +1,49 @@
include Math include Math
def follow_instructions(input) Instruction = Struct.new(:direction, :blocks) do
seq = input.split(', ') def initialize(direction, blocks)
super(direction, blocks.to_i)
end
end
# start facing north at the origin Turtle = Struct.new(:orientation, :location) do
dir = -PI/2 def self.run(input)
locations = [[0,0]] instructions = parse(input)
seq.each.with_object(locations) do |step, locations| turtles = [Turtle.new(-PI/2, [0,0])]
/(?<turn>\w)(?<blocks>\d+)/ =~ step
case turn instructions.each.with_object(turtles) do |instruction, turtles|
when ?R turtles << turtles.last.turn(instruction.direction).step
dir += PI/2 (instruction.blocks - 1).times do
when ?L turtles << turtles.last.step
dir -= PI/2 end
else
raise "unexpected turn: #{turn}"
end end
blocks.to_i.times do turtles
location = locations.last
x = location[0] + cos(dir).to_i
y = location[1] + sin(dir).to_i
locations << [x, y]
end
end end
locations def self.parse(input)
end input.split(', ').map {|step|
Instruction.new(*step.scan(/(?<turn>\w)(?<blocks>\d+)/)[0])
}
end
def turn(direction)
angle = case direction
when ?R
PI/2
when ?L
-PI/2
else
raise "unexpected direction: #{direction}"
end
Turtle.new(orientation + angle, location)
end
def first_dupe(ary) def step
seen = Set.new x = location[0] + cos(orientation).to_i
ary.find { |elem| y = location[1] + sin(orientation).to_i
seen.include?(elem) || (seen << elem && false) Turtle.new(orientation, [x, y])
} end
end end
require 'minitest' require 'minitest'
@ -45,23 +55,17 @@ class TestInstructions < Minitest::Test
assert_distance 12, 'R5, L5, R5, R3' assert_distance 12, 'R5, L5, R5, R3'
end end
def test_first_dupe
assert_equal 1, first_dupe([1, 2, 3, 1])
assert_equal 2, first_dupe([1, 2, 3, 2])
assert_equal 3, first_dupe([1, 2, 3, 3])
locations = follow_instructions('R8, R4, R4, R8')
assert_equal [4, 0], first_dupe(locations)
end
def assert_distance(expected, instructions) def assert_distance(expected, instructions)
locations = follow_instructions(instructions) assert_equal expected, Turtle.run(instructions).last.location.map(&:abs).inject(:+)
assert_equal expected, locations.last.map(&:abs).inject(:+)
end end
end end
if __FILE__ == $0 if __FILE__ == $0
puts first_dupe(follow_instructions(DATA.read)) turtles = Turtle.run(DATA.read)
p turtles.last
locations = turtles.map(&:location)
p locations.find {|location| locations.count(location) > 1 }
end end
__END__ __END__

Loading…
Cancel
Save