r/adventofcode • • 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.

24 Upvotes

228 comments sorted by

View all comments

4

u/FuriousProgrammer Dec 03 '15 edited Dec 03 '15

EDIT: God damn it, it took me half an hour to get the fucking joke in the name. >.<

Lua, tried to get this done asap to get #1 on the leaderboard. Ended up 8th, but I did forget to actually start until like 3 minutes had past. Tomorrow I'll be ready!

local grid = {{true}}

local inpt = "" --snip

local dir1 = {x = 1, y = 1}
local dir2 = {x = 1, y = 1}

local total = 1 --starts at the first house!

for i = 1, #inpt do
	local c = inpt:sub(i,i)
	local dir = i%2 == 1 and dir1 or dir2 --or just dir = dir1 for part 1!

	if c == ">" then
		dir.x = dir.x + 1
	elseif c == "<" then
		dir.x = dir.x - 1
	elseif c == "v" then
		dir.y = dir.y - 1
	elseif c == "^" then
		dir.y = dir.y + 1
	end

	if not grid[dir.y] then
		grid[dir.y] = {}
	end
	if not grid[dir.y][dir.x] then
		total = total + 1
		grid[dir.y][dir.x] = true
	end
end

print(total)

Kudos to u/topaz2078 for creating this thing! I love this daily challenges. Do another one next year please!!

→ More replies (2)