Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Created November 14, 2017 17:00
Show Gist options
  • Save dengsauve/f1ab6d0e99c1e7ef3fed2911a1387f39 to your computer and use it in GitHub Desktop.
Save dengsauve/f1ab6d0e99c1e7ef3fed2911a1387f39 to your computer and use it in GitHub Desktop.
AoC 2016 Day 1: No Time for a Taxicab created by dengsauve - https://repl.it/@dengsauve/AoC-2016-Day-1-No-Time-for-a-Taxicab
text = "R4, R4, L1, R3, L5, R2, R5, R1, L4, R3, L5, R2, L3, L4, L3, R1, R5, R1, L3, L1, R3, L1, R2, R2, L2, R5, L3, L4, R4, R4, R2, L4, L1, R5, L1, L4, R4, L1, R1, L2, R5, L2, L3, R2, R1, L194, R2, L4, R49, R1, R3, L5, L4, L1, R4, R2, R1, L5, R3, L5, L4, R4, R4, L2, L3, R78, L5, R4, R191, R4, R3, R1, L2, R1, R3, L1, R3, R4, R2, L2, R1, R4, L5, R2, L2, L4, L2, R1, R2, L3, R5, R2, L3, L3, R3, L1, L1, R5, L4, L4, L2, R5, R1, R4, L3, L5, L4, R5, L4, R5, R4, L3, L2, L5, R4, R3, L3, R1, L5, R5, R1, L3, R2, L5, R5, L3, R1, R4, L5, R4, R2, R3, L4, L5, R3, R4, L5, L5, R4, L4, L4, R1, R5, R3, L1, L4, L3, L4, R1, L5, L1, R2, R2, R4, R4, L5, R4, R1, L1, L1, L3, L5, L2, R4, L3, L5, L4, L1, R3"
directions = text.split(", ")
ox = 0
dx = 0
oy = 1
dy = 0
directions.each do | d |
# Change directions
m = 1
m = -1 unless d.include?("R")
if ox == 0
oy == 1 ? ox = (1 * m) : ox = (-1 * m)
oy = 0
else
ox == 1 ? oy = (-1 * m) : oy = (1 * m)
ox = 0
end
#Go the distance
distance = d[1..-1].to_i
dx += (distance * ox)
dy += (distance * oy)
end
puts "#{dx.abs} blocks #{dx.positive? ? 'east' : 'west'}",
"#{dy.abs} blocks #{dy.positive? ? 'north' : 'south'}",
"#{dx.abs + dy.abs} blocks away"
# Answer should be 61, -85, 146 (refactoring after solved)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment