r/adventofcode • • Dec 12 '15

SOLUTION MEGATHREAD --- Day 12 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

edit: Leaderboard capped, thread unlocked!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 12: JSAbacusFramework.io ---

Post your solution as a comment. Structure your post like previous daily solution threads.

7 Upvotes

183 comments sorted by

View all comments

Show parent comment

2

u/randrews Dec 12 '15

Actually, if you consider the use of a json parser cheating (it's not actually in the Lua stdlib) this also works:

file = io.open('12-data.txt')
input = file:read("*a")
file:close()

function sum_str(str)
    local sum = 0
    for num in str:gmatch("([-]?%d+)") do
        sum = sum + tonumber(num)
    end
    return sum
end

function cut_red(str)
    local left, right = str:find(":\"red\"")
    local left_brace = nil
    local right_brace = nil
    local stack = 0

    for i=left, 1, -1 do
        local c = str:sub(i,i)
        if c == "}" then stack = stack + 1
        elseif c == "{" and stack > 0 then stack = stack - 1
        elseif c == "{" and stack == 0 then
            left_brace = i
            break
        end
    end

    stack = 0
    for i=right, #str do
        local c = str:sub(i,i)
        if c == "{" then stack = stack + 1
        elseif c == "}" and stack > 0 then stack = stack - 1
        elseif c == "}" and stack == 0 then
            right_brace = i
            break
        end
    end

    local new_str = str:sub(1, left_brace) .. str:sub(right_brace, #str)
    if new_str:find(":\"red\"") then return cut_red(new_str)
    else return new_str end
end

print("Part 1:", sum_str(input))
print("Part 2:", sum_str(cut_red(input)))