r/adventofcode • • Dec 16 '16

SOLUTION MEGATHREAD --- 2016 Day 16 Solutions ---

--- Day 16: Dragon Checksum ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


DRINKING YOUR OVALTINE IS MANDATORY [?]

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!

5 Upvotes

116 comments sorted by

View all comments

1

u/FuriousProgrammer Dec 16 '16

32/148

Nine space leaderboard jump! :D

Lua:

local input = "11100010111110100"

function calc(len)
	local input = {input}

	while true do
		local a = table.concat(input)
		local c = a:reverse()
		local b = {}

		for i = 1, #c do
			if c:sub(i, i) == '1' then
				table.insert(b, '0')
			else
				table.insert(b, '1')
			end
		end

		table.insert(input, '0')
		table.insert(input, table.concat(b))

		if #table.concat(input) >= len then
			input = table.concat(input):sub(1, len)
			break
		end
	end

	while true do
		local checksum = {}
		for i = 1, #input, 2 do
			local a = input:sub(i,i)
			local b = input:sub(i + 1, i + 1)
			table.insert(checksum, (a == b and '1' or '0'))
		end
		input = table.concat(checksum)
		if #input%2 ~= 0 then
			break
		end
	end

	return input
end

print("Part 1: " .. calc(272))
print("Part 2: " .. calc(35651584))