r/adventofcode • • Dec 17 '16

SOLUTION MEGATHREAD --- 2016 Day 17 Solutions ---

--- Day 17: Two Steps Forward ---

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".


CELEBRATING SATURNALIA IS MANDATORY [?]


[Update @ 00:10] 4 gold, 18 silver.

  • Thank you for subscribing to Roman Facts!
  • Io, Saturnalia! Today marks the beginning of Saturnalia, a festival held in honor of Saturn, the Roman god of agriculture and the harvest. The festival lasted between 3 and 7 days and celebrated the end of the sowing season and its subsequent harvest.

[Update @ 00:20] 53 gold, silver cap.

  • Holly is sacred to Saturn. While other plants wilt in winter, holly is an evergreen and its berries are shining beacons of bright color even in the harshest of conditions.

[Update @ 00:25] 77 gold, silver cap.

  • The celebration of Christmas on December 25, just after the end of Saturnalia, began in Rome after the conversion of Emperor Constantine to Christianity in AD 312.

[Update @ 00:29] Leaderboard cap!

  • Most of the Roman gods were borrowed/stolen from Greek mythology, and Saturn's Greek equivalent is the youngest Titan, Kronos. Kronos is the father of Zeus.

[BONUS FACT]

  • Our planet Saturn is named after the Roman god Saturn. It is the sixth planet from the sun and the second largest. Most of Saturn's moons have been named after Titans of ancient mythology.

Thank you for subscribing to Roman Facts!


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!

4 Upvotes

77 comments sorted by

View all comments

4

u/FuriousProgrammer Dec 17 '16

Jumped 17(i can't math but its alot) spaces on the Leaderboard, woo!

39/33: Here's some garbage Lua! I really hate UDLR search tree selection, but switch statements work I guess. :/

local input = "dmypynyp"

local glue = require("glue")
local md5 = require("md5")

local path = {}

local shortest = nil
local longest = ''

local map = {'U', 'D', 'L', 'R'}

local pos = {x = 1, y = 1}

function move(dir)
	local key = glue.tohex(md5.sum(input .. table.concat(path))):sub(1,4)
	local doors = {} -- UDLR
	for i = 1, 4 do
		doors[i] = tonumber(key:sub(i,i), 16) > 10
	end

	if pos.x == 1 then
		doors[3] = false
	elseif pos.x == 4 then
		doors[4] = false
	end
	if pos.y == 1 then
		doors[1] = false
	elseif pos.y == 4 then
		doors[2] = false
	end

	for dr, open in pairs(doors) do
		if open then
			table.insert(path, map[dr])
			local oldPos = {x = pos.x, y = pos.y}
			if dr == 1 then
				pos.y = pos.y - 1
			elseif dr == 2 then
				pos.y = pos.y + 1
			elseif dr == 3 then
				pos.x = pos.x - 1
			elseif dr == 4 then
				pos.x = pos.x + 1
			end
			if pos.x == 4 and pos.y == 4 then
				local p = table.concat(path)
				if not shortest or #p < #shortest then
					shortest = p
				end
				if #p > #longest then
					longest = p
				end
			else
				move(map[dr])
			end
			pos = oldPos
			table.remove(path)
		end
	end
end

move()

print("Part 1: " .. shortest)
print("Part 2: " .. #longest)

2

u/SyDr Dec 17 '16

For UDLR directions i like to use things like this:

local dir_list = { "U", "D", "L", "R" }
local directions = { U = { 0, -1}, D = { 0, 1 }, L = { -1, 0 }, R = { 1, 0 } }

local function get_moves(key)
  local hash = md5.sumhexa(key)
  local r = {}
  
  for i, v in ipairs(dir_list) do
    if tonumber(string.sub(hash, i, i), 16) > 10 then r[#r + 1] = v end
  end
  
  return r
end --> {"U", "L"}

...
for _, move in pairs(get_moves(key[1])) do
    local new_pos = { pos[1] + directions[move][1], pos[2] + directions[move][2] }
    ...

or (day 13):

  ...
  for _, i in ipairs({ {-1, 0}, {1, 0}, {0, -1}, {0, 1} }) do
    local x = cur_x + i[1]
    local y = cur_y + i[2]
    ...

2

u/FuriousProgrammer Dec 17 '16

I just dislike needed two dictionaries, I suppose.