Refactoring

This commit is contained in:
Sven Weidauer 2022-12-10 17:04:33 +01:00
parent 6c3937e4f8
commit 6b96700e2e

View file

@ -1,12 +1,5 @@
require "set"
$headX = 0
$headY = 0
$tailX = 0
$tailY = 0
$tailPositions = Set[[$tailX, $tailY]]
def moveStep(dx, dy)
$headX += dx
$headY += dy
@ -35,6 +28,14 @@ def moveStep(dx, dy)
end
def run count
$headX = 0
$headY = 0
$tailX = 0
$tailY = 0
$tailPositions = Set[[$headX, $headY]]
for line in File.readlines('day9.input')
unless /^(?<dir>[RLUD]) (?<steps>\d+)$/ =~ line
puts "Failed to parse #{line}"
@ -52,11 +53,12 @@ for line in File.readlines('day9.input')
steps.times { moveStep 0, 1 }
when "D" then
steps.times { moveStep 0, -1 }
else
raise
end
end
puts $tailPositions.count
$tailPositions.count
end
puts "Part 1: #{run(2)}"