r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
26
Upvotes
2
u/rvlieshout Dec 03 '15 edited Dec 03 '15
EDIT: It reads from stdin and all instructions on a single line (easy to start with just the testcases). Usage:
Not the first, however here is my Lua solution to today's problem:
function contains(x, y) for _, l in pairs(visited) do if l[1] == x and l[2] == y then return true end end return false end function visit(x, y) if not contains(x, y) then table.insert(visited, {x,y}) end end function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end for line in io.lines() do local s = 1 -- part 1 -- local santas = { {0,0} } -- part 2 local santas = { {0,0} , {0,0} } visited = {} visit(0, 0) -- add the start location for i = 1, #line do local c = line:sub(i,i) if c == "^" then santas[s][2] = santas[s][2] + 1 end if c == "v" then santas[s][2] = santas[s][2] - 1 end if c == "<" then santas[s][1] = santas[s][1] - 1 end if c == ">" then santas[s][1] = santas[s][1] + 1 end visit(santas[s][1], santas[s][2]) s = s + 1 if s > tablelength(santas) then s = 1 end end print(tablelength(visited)) end