Skip to content

Commit

Permalink
2016: Add day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jp7677 committed Dec 20, 2024
1 parent 3eaf28d commit c136653
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
2 changes: 2 additions & 0 deletions 2016/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Metrics:
Enabled: false
52 changes: 47 additions & 5 deletions 2016/test/day01_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def self.turn_left(direction)
end
end

Step = Data.define(:direction, :position)

describe 'day01' do
it 'solves part 01' do
steps = Util.read_input('day01-input.txt').first.split(', ')
Expand All @@ -41,17 +43,57 @@ def self.turn_left(direction)
when 'L'then Directions.turn_left(acc[0])
else raise('invalid value')
end
step_count = it[1..].to_i

step_numbers = it[1..].to_i
case direction
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]]
when Directions::NORTH then [direction, [acc[1][0] + step_count, acc[1][1]]]
when Directions::EAST then [direction, [acc[1][0], acc[1][1] + step_count]]
when Directions::SOUTH then [direction, [acc[1][0] - step_count, acc[1][1]]]
when Directions::WEST then [direction, [acc[1][0], acc[1][1] - step_count]]
else raise('invalid value')
end
end

assert_equal 279, result[1][0] + result[1][1]
end

it 'solves part 02' do
steps = Util.read_input('day01-input.txt').first.split(', ')

path = steps.reduce [Step.new(Directions::NORTH, [0, 0])] do |acc, it|
prev = acc.last
direction =
case it[0]
when 'R' then Directions.turn_right(prev.direction)
when 'L'then Directions.turn_left(prev.direction)
else raise('invalid value')
end
step_count = it[1..].to_i

case direction
when Directions::NORTH
acc + (prev.position[0] + 1..prev.position[0] + step_count).map do |p|
Step.new(direction, [p, prev.position[1]])
end
when Directions::EAST
acc + (prev.position[1] + 1..prev.position[1] + step_count).map do |p|
Step.new(direction, [prev.position[0], p])
end
when Directions::SOUTH
acc + (prev.position[0] - 1..prev.position[0] - step_count).step(-1).map do |p|
Step.new(direction, [p, prev.position[1]])
end
when Directions::WEST
acc + (prev.position[1] - 1..prev.position[1] - step_count).step(-1).map do |p|
Step.new(direction, [prev.position[0], p])
end
else raise('invalid value')
end
end

positions = path.map(&:position)
duplicated = positions.detect { |p| positions.count(p) > 1 }

assert_equal 163, duplicated[0] + duplicated[1]
end
end

0 comments on commit c136653

Please sign in to comment.