r/adventofcode • • Dec 07 '16

SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---

From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!

Show this overgrown furball what you've got!


--- Day 7: Internet Protocol Version 7 ---

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


ALWAYS DIGGING STRAIGHT DOWN 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!

14 Upvotes

180 comments sorted by

View all comments

Show parent comment

3

u/AudriusU Dec 07 '16

Lua rocks, especially the power of search patterns ;)

ipv7list = {
"abba[mnop]qrst",
"abcd[bddb]xyyx",
"aaaa[qwer]tyui",
"aba[bab]xyz"}

cnt1,cnt2 = 0,0
for _,s in ipairs(ipv7list) do
    local ss = s:gsub("%b[]", " ")
    for q1,q2 in ss:gmatch("(%a)(%a)%2%1") do
        if q1 ~= q2 then
            local nqFound = false
            for nq1, nq2 in s:gmatch("%[%a*(%a)(%a)%2%1%a*%]") do
                if nq1 ~= nq2 then
                    nqFound = true
                    break
                end
            end
            if not nqFound then
                cnt1 = cnt1 + 1
            end
            break
        end
    end
    for i = 1,ss:len() - 2 do
        local n,_,q1,q2 = ss:find("(%a)(%a)%1", i)
        if n and q1 ~= q2 and s:find("%[%a*"..q2..q1..q2.."%a*%]") then
            cnt2= cnt2 + 1
            break
        end
    end
end

print("ABBA:", cnt1)
print("ABA:", cnt2)

1

u/FuriousProgrammer Dec 07 '16

Like I said, string patterns are not my forté. :P

I had no idea the (%a)(%a)%1 mechanism existed, definitely would have saved me a few lines of code.