Skip to content

Instantly share code, notes, and snippets.

@gurgeous
Created December 12, 2020 05:37
Show Gist options
  • Save gurgeous/925b29e43f1f0203985f40bb799fd752 to your computer and use it in GitHub Desktop.
Save gurgeous/925b29e43f1f0203985f40bb799fd752 to your computer and use it in GitHub Desktop.
# 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