Skip to content

Instantly share code, notes, and snippets.

@gurgeous
Created December 12, 2020 06:43
Show Gist options
  • Save gurgeous/a719b780fa5b117ec5a8f6ede5280985 to your computer and use it in GitHub Desktop.
Save gurgeous/a719b780fa5b117ec5a8f6ede5280985 to your computer and use it in GitHub Desktop.
# part 1
p, h = 0.to_c, 1.to_c
data.lines.each do |s|
d = s[1..].to_i
case s[0]
when 'N' then p += d * 1i
when 'S' then p += d * -1i
when 'E' then p += d
when 'W' then p -= d
when 'L' then (d / 90).times { h *= 1i }
when 'R' then (d / 90).times { h *= -1i }
when 'F' then p += h * d
end
end
puts p.real.abs + p.imag.abs
# part 2
s, p = 0.to_c, 10 + 1i
data.lines.each do |line|
d = line[1..].to_i
case line[0]
when 'N' then p += d * 1i
when 'S' then p += -d * 1i
when 'E' then p += d
when 'W' then p -= d
when 'L' then (d / 90).times { p *= 1i }
when 'R' then (d / 90).times { p *= -1i }
when 'F' then s += p * d
end
end
puts s.real.abs + s.imag.abs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment