2022-12-10 16:43:40 +01:00
|
|
|
require "set"
|
|
|
|
|
|
|
|
def moveStep(dx, dy)
|
|
|
|
$headX += dx
|
|
|
|
$headY += dy
|
|
|
|
|
2022-12-10 17:16:13 +01:00
|
|
|
(curX, curY) = [$headX, $headY]
|
2022-12-10 16:43:40 +01:00
|
|
|
|
2022-12-10 17:16:13 +01:00
|
|
|
$tail.each_index { |index|
|
|
|
|
(tailX, tailY) = $tail[index]
|
|
|
|
|
|
|
|
dx = curX - tailX
|
|
|
|
dy = curY - tailY
|
2022-12-10 16:43:40 +01:00
|
|
|
|
2022-12-10 17:16:13 +01:00
|
|
|
case [dx.abs, dy.abs]
|
|
|
|
when [0, 0], [1, 1], [1, 0], [0, 1] then
|
|
|
|
# Stay the same
|
|
|
|
when [2, 0], [0, 2], [2, 2] then
|
|
|
|
tailX += dx <=> 0
|
|
|
|
tailY += dy <=> 0
|
|
|
|
when [2, 1] then
|
|
|
|
tailX += dx <=> 0
|
|
|
|
tailY += dy <=> 0
|
|
|
|
when [1, 2] then
|
|
|
|
tailX += dx <=> 0
|
|
|
|
tailY += dy <=> 0
|
|
|
|
else
|
|
|
|
puts "Unknown: (#{dx}, #{dy})"
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
(curX, curY) = $tail[index] = [tailX, tailY]
|
|
|
|
}
|
2022-12-10 16:43:40 +01:00
|
|
|
|
2022-12-10 17:16:13 +01:00
|
|
|
$tailPositions.add [curX, curY]
|
2022-12-10 16:43:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-12-10 17:04:33 +01:00
|
|
|
def run count
|
|
|
|
$headX = 0
|
|
|
|
$headY = 0
|
|
|
|
|
2022-12-10 17:16:13 +01:00
|
|
|
$tail = [[$headX, $headY]] * (count - 1)
|
|
|
|
|
2022-12-10 17:04:33 +01:00
|
|
|
$tailPositions = Set[[$headX, $headY]]
|
|
|
|
|
|
|
|
for line in File.readlines('day9.input')
|
|
|
|
unless /^(?<dir>[RLUD]) (?<steps>\d+)$/ =~ line
|
|
|
|
puts "Failed to parse #{line}"
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
steps = steps.to_i
|
|
|
|
|
|
|
|
case dir
|
|
|
|
when "R" then
|
|
|
|
steps.times { moveStep 1, 0 }
|
|
|
|
when "L" then
|
|
|
|
steps.times { moveStep -1, 0 }
|
|
|
|
when "U" then
|
|
|
|
steps.times { moveStep 0, 1 }
|
|
|
|
when "D" then
|
|
|
|
steps.times { moveStep 0, -1 }
|
|
|
|
end
|
2022-12-10 16:43:40 +01:00
|
|
|
end
|
|
|
|
|
2022-12-10 17:04:33 +01:00
|
|
|
$tailPositions.count
|
2022-12-10 16:43:40 +01:00
|
|
|
end
|
|
|
|
|
2022-12-10 17:04:33 +01:00
|
|
|
puts "Part 1: #{run(2)}"
|
2022-12-10 17:16:13 +01:00
|
|
|
puts "Part 2: #{run(10)}"
|
2022-12-10 16:43:40 +01:00
|
|
|
|