70 lines
1.5 KiB
Ruby
Executable File
70 lines
1.5 KiB
Ruby
Executable File
#!/bin/env ruby
|
|
require 'pry'
|
|
|
|
def turn_ship (direction, degree, facing)
|
|
dirs = [:north, :east, :south, :west]
|
|
degree = degree / 90
|
|
degree = -degree if direction == 'L'
|
|
spin = dirs.find_index(facing) + degree
|
|
spin -= 4 if spin > 3
|
|
spin += 4 if spin < 0
|
|
return dirs[spin]
|
|
end
|
|
|
|
def rotate (direction, degree, waypoint)
|
|
degree = degree / 90
|
|
degree.times do
|
|
waypoint[:x_position], waypoint[:y_position] =
|
|
waypoint[:y_position], (waypoint[:x_position])
|
|
|
|
waypoint[:y_position] *= -1 if direction =='R'
|
|
waypoint[:x_position] *= -1 if direction == 'L'
|
|
end
|
|
return waypoint
|
|
end
|
|
|
|
|
|
ship = {
|
|
:facing => :east,
|
|
:x_position => 0,
|
|
:y_position => 0
|
|
}
|
|
|
|
waypoint = {
|
|
:x_position => 10,
|
|
:y_position => 1
|
|
}
|
|
|
|
directions = File.read('./input').split("\n")
|
|
|
|
directions.each do |direction|
|
|
instruction = direction[0]
|
|
count = direction[1..-1].to_i
|
|
|
|
p [instruction, count]
|
|
case instruction
|
|
when 'F'
|
|
count.times do
|
|
ship[:x_position] += waypoint[:x_position]
|
|
ship[:y_position] += waypoint[:y_position]
|
|
end
|
|
when 'N'
|
|
waypoint[:y_position] += count
|
|
when 'S'
|
|
waypoint[:y_position] -= count
|
|
when 'E'
|
|
waypoint[:x_position] += count
|
|
when 'W'
|
|
waypoint[:x_position] -= count
|
|
when 'R','L'
|
|
waypoint = rotate(instruction, count, waypoint)
|
|
end
|
|
pp ship
|
|
end
|
|
|
|
manhattan = ship[:x_position].abs + ship[:y_position].abs
|
|
|
|
p manhattan
|
|
|
|
|
|
#binding.pry |