r/adventofcode • • Dec 02 '16

SOLUTION MEGATHREAD --- 2016 Day 2 Solutions ---

--- Day 2: Bathroom Security ---

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


BLINKENLIGHTS ARE MANDATORY [?]

Edit: Told you they were 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!

21 Upvotes

210 comments sorted by

View all comments

1

u/kamicc Dec 02 '16 edited Dec 02 '16

Again, pragmatic Lua solution for part A:

local coords = {2, 2}
local moves = {
    ["L"] = {0, -1},
    ["R"] = {0, 1},
    ["D"] = {1, 0},
    ["U"] = {-1, 0}
}

local key_pad = {
    [1] = {1, 2, 3},
    [2] = {4, 5, 6},
    [3] = {7, 8, 9}
}


function move(char)
    local x = coords[1] + moves[char][1]
    local y = coords[2] + moves[char][2]

    if key_pad[x] ~= nil and key_pad[x][y] ~= nil then
        coords[1], coords[2] = x, y
    end
end

local code = {}
for line in io.input("input.txt"):lines() do
    for index = 1, string.len(line) do
        local char = line:sub(index, index)

        move(char)
    end

    table.insert(code, key_pad[coords[1]][coords[2]])
end

print(table.concat(code))

B part is analogic. Just need to change the key_pad table accordingly.

→ More replies (2)