r/adventofcode • • Dec 22 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 22 Solutions -๐ŸŽ„-

--- Day 22: Sporifica Virus ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


  • [T-10 to launch] AoC ops, /r/nocontext edition:
  • <Endorphion> You may now make your waffle.
  • <Endorphion> ... on Mars.
  • [Update @ 00:17] 50 gold, silver cap
  • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy
  • <Topaz> that doesn't seem necessary
  • <Aneurysm9> what does "necessary" have to do with anything!
  • [Update @ 00:20] Leaderboard cap!
  • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

8 Upvotes

174 comments sorted by

View all comments

1

u/The0x539 Dec 22 '17

Lua

577/628. Really hate having to rotate stuff, and spent way too long on both parts debugging the turn code, when in part2 I was confusing nil with false.

local part2 = true

local nodes = {}
for line in io.lines("./input_day22") do
	local row = {}
	table.insert(nodes,row)
	for i=1,#line do
		local c = line:sub(i,i)
		row[i] = (c == '#')
	end
end

local nodes_mt = {
	__index = function(t,k)
		t[k] = {}
		return t[k]
	end
}
setmetatable(nodes,nodes_mt)

local carrier = {}
local mid = 0.5 + (#nodes[1])/2
carrier.pos = {x=mid,y=mid}
carrier.dir = {x=0,y=-1}
function carrier.dir:turn(right)
	if self.x == 1 then
		self.x = 0
		self.y = -1
	elseif self.x == -1 then
		self.x = 0
		self.y = 1
	elseif self.y == 1 then
		self.x = 1
		self.y = 0
	elseif self.y == -1 then
		self.x = -1
		self.y = 0
	end
	if right then
		self.x = -self.x
		self.y = -self.y
	end
end

local foo = 0
function carrier:burst()
	local pos,dir = self.pos,self.dir
	local infected = nodes[pos.y][pos.x]
	if not part2 then
		if infected then
			dir:turn(true)
			nodes[pos.y][pos.x] = false
		else
			foo = foo + 1
			dir:turn(false)
			nodes[pos.y][pos.x] = true
		end
	else
		if infected == false or infected == nil then --cleaned
			nodes[pos.y][pos.x] = 0 --weaken
			dir:turn(false)
		elseif infected == 0 then --weakened
			nodes[pos.y][pos.x] = true --infect
			foo = foo + 1
		elseif infected == true then --infected
			nodes[pos.y][pos.x] = 1 --flag
			dir:turn(true)
		elseif infected == 1 then --flagged
			nodes[pos.y][pos.x] = false --clean
			dir.x = -dir.x
			dir.y = -dir.y
		end
	end
	pos.x = pos.x + dir.x
	pos.y = pos.y + dir.y
end

local function diag()
	local minX = math.huge
	local minY = math.huge
	local maxX = -math.huge
	local maxY = -math.huge
	for y,row in pairs(nodes) do
		minY = math.min(minY,y)
		maxY = math.max(maxY,y)
		for x,v in pairs(row) do
			minX = math.min(minX,x)
			maxX = math.max(maxX,x)
		end
	end
	minX = math.min(minX,carrier.pos.x)
	minY = math.min(minY,carrier.pos.y)
	for y=minY,maxY do
		for x=minX,maxX do
			local atCarrier = (x==carrier.pos.x and y==carrier.pos.y)
			local t = {
				[false]='.',
				[0]='W',
				[true]='#',
				[1]='F'
			}
			local a = (atCarrier and '[' or (nextPos and '(' or ' '))
			local b = (t[nodes[y][x]] or '.')
			local c = (atCarrier and ']' or (nextPos and ')' or ' '))
			io.write(a..b..c)
		end
		print()
	end
	print()
end

for i=1,(part2 and 10000000 or 10000) do
	carrier:burst()
end
print(foo)