Created
December 12, 2020 05:37
-
-
Save gurgeous/925b29e43f1f0203985f40bb799fd752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# part 1 | |
x = y = deg = 0 | |
data.lines.each do |s| | |
d = s[1..].to_i | |
case s[0] | |
when 'N' then y += d | |
when 'S' then y -= d | |
when 'E' then x += d | |
when 'W' then x -= d | |
when 'L' then deg += d | |
when 'R' then deg -= d | |
when 'F' | |
case deg % 360 | |
when 0 then x += d | |
when 90 then y += d | |
when 180 then x -= d | |
when 270 then y -= d | |
end | |
end | |
end | |
p x.abs + y.abs | |
# part 2 | |
sx = sy = 0 | |
wx, wy = 10, 1 | |
data.lines.each do |s| | |
d = s[1..].to_i | |
case s[0] | |
when 'N' then wy += d | |
when 'S' then wy -= d | |
when 'E' then wx += d | |
when 'W' then wx -= d | |
when 'L' then (d / 90).times { wx, wy = -wy, wx } | |
when 'R' then (d / 90).times { wx, wy = wy, -wx } | |
when 'F' then sx += wx * d; sy += wy * d | |
end | |
end | |
puts sx.abs + sy.abs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment