Skip to content

Instantly share code, notes, and snippets.

@kamiccolo
Created December 2, 2016 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiccolo/97e7c8a0e0bc47cd207b7739f597ebe2 to your computer and use it in GitHub Desktop.
Save kamiccolo/97e7c8a0e0bc47cd207b7739f597ebe2 to your computer and use it in GitHub Desktop.
Advent of Code 2016
local content = io.input("input.txt"):read("*a")
local coords = {0, 0}
local direction = {0, 1}
local visited = {}
visited[0] = {}
visited[0][0] = true
function check_overlap(old_coords, new_coords, direction)
local x = old_coords[1]
local y = old_coords[2]
repeat
x = x + direction[1]
y = y + direction[2]
if visited[x] == nil then
visited[x] = {}
end
if visited[x][y] == nil then
visited[x][y] = true
else
return math.abs(x) + math.abs(y)
end
until x == new_coords[1] and y == new_coords[2]
return false
end
for turn, steps in content:gmatch("(%a)(%d+)") do
steps = tonumber(steps)
if turn == "L" then
direction[1], direction[2] = -direction[2], direction[1]
else
direction[1], direction[2] = direction[2], -direction[1]
end
local new_coords = {
coords[1] + direction[1] * steps,
coords[2] + direction[2] * steps
}
local overlap = check_overlap(coords, new_coords, direction)
if overlap then
print("Found overlap at: ", overlap)
end
coords = new_coords
end
print("Finishing location: ", coords[1], coords[2])
print("Distance: ", math.abs(coords[1]) + math.abs(coords[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment