-
-
Save kamiccolo/97e7c8a0e0bc47cd207b7739f597ebe2 to your computer and use it in GitHub Desktop.
Advent of Code 2016
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
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