Skip to content

Commit

Permalink
Improvement: Better Year 2016 Day 01
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 19, 2024
1 parent d5eac44 commit 8e87caf
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions year_2016/day_01.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@ def initialize(input_data, input_part_one = false)
@version = input_part_one ? 1 : 2
@direction = 0
@position = [0, 0]
@visited = [[0, 0]]
@visited = Set.new([0, 0])
do_all_moves(input_data)
end

# rubocop:disable Lint/NonLocalExitFromIterator
def do_all_moves(instructions)
instructions.split(', ').each do |instruction|
one_turn(instruction[0])
moves = instruction.scan(/\d+/)[0].to_i
@direction = (@direction + (instruction[0] == 'L' ? 3 : 5)) % 4
moves = instruction[1..].to_i
next one_move(moves) if @version == 1

1.upto(moves) do
one_move(1)
@visited.include?(@position) ? break : @visited.push(@position.dup)
return if @visited.include?(@position)

@visited.add(@position.dup)
end
break if @visited[..-2].include?(@position)
end
end

def one_turn(leftright)
@direction = (@direction + 4 + (leftright == 'L' ? -1 : 1)) % 4
end
# rubocop:enable Lint/NonLocalExitFromIterator

def one_move(moves)
case DIRECTIONS[@direction]
Expand Down

0 comments on commit 8e87caf

Please sign in to comment.