Skip to content

Commit

Permalink
2016: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jp7677 committed Dec 20, 2024
1 parent ffc92bd commit cf83d0c
Showing 1 changed file with 22 additions and 40 deletions.
62 changes: 22 additions & 40 deletions 2016/test/day01_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,46 @@ class Directions
SOUTH = 3
WEST = 4

def self.turn_right(direction) # rubocop:disable Metrics/MethodLength
def self.turn_right(direction)
case direction
when NORTH
EAST
when EAST
SOUTH
when SOUTH
WEST
when WEST
NORTH
else
raise('invalid value')
when NORTH then EAST
when EAST then SOUTH
when SOUTH then WEST
when WEST then NORTH
else raise('invalid value')
end
end

def self.turn_left(direction) # rubocop:disable Metrics/MethodLength
def self.turn_left(direction)
case direction
when NORTH
WEST
when WEST
SOUTH
when SOUTH
EAST
when EAST
NORTH
else
raise('invalid value')
when NORTH then WEST
when WEST then SOUTH
when SOUTH then EAST
when EAST then NORTH
else raise('invalid value')
end
end
end

describe 'day01' do # rubocop:disable Metrics/BlockLength
it 'solves part 01' do # rubocop:disable Metrics/BlockLength
describe 'day01' do
it 'solves part 01' do
steps = Util.read_input('day01-input.txt').first.split(', ')

result = steps.reduce [Directions::NORTH, [0, 0]] do |acc, it|
direction =
case it[0]
when 'R'
Directions.turn_right(acc[0])
when 'L'
Directions.turn_left(acc[0])
else
raise('invalid value')
when 'R' then Directions.turn_right(acc[0])
when 'L'then Directions.turn_left(acc[0])
else raise('invalid value')
end

step_numbers = it[1..].to_i
case direction
when Directions::NORTH
[direction, [acc[1][0] + step_numbers, acc[1][1]]]
when Directions::EAST
[direction, [acc[1][0], acc[1][1] + step_numbers]]
when Directions::SOUTH
[direction, [acc[1][0] - step_numbers, acc[1][1]]]
when Directions::WEST
[direction, [acc[1][0], acc[1][1] - step_numbers]]
else
raise('invalid value')
when Directions::NORTH then [direction, [acc[1][0] + step_numbers, acc[1][1]]]
when Directions::EAST then [direction, [acc[1][0], acc[1][1] + step_numbers]]
when Directions::SOUTH then [direction, [acc[1][0] - step_numbers, acc[1][1]]]
when Directions::WEST then [direction, [acc[1][0], acc[1][1] - step_numbers]]
else raise('invalid value')
end
end

Expand Down

0 comments on commit cf83d0c

Please sign in to comment.