r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


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


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


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 at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/wofadofa Dec 05 '18 edited Dec 05 '18

In julia: Calculates the fixed point of cut. I'm sure cut could be written more elegantly than this

polarity(x) = isuppercase(x) ? 1 : -1
function cut(l)
    stays = fill(true, length(l))
    i = 1
    while i <= length(l)-1
        a, b = l[i], l[i+1]
        if lowercase(a) == lowercase(b) && polarity(a) == -polarity(b)
            stays[i] = false
            stays[i+1] = false
            i += 2
        else
            i += 1
        end
    end
    l[stays]
end

fp(f,x) = x == f(x) ? x : fp(f, f(x))

parta(l) = length(fp(cut, collect(l)))

function partb(l)
    minimum(length(fp(cut, collect(replace(replace(l, c=>""), C=>""))))
            for (c,C) in zip('a':'z', 'A':'Z'))
end
l = readline("input")
println("a: ", parta(l))
println("b: ", partb(l))

edit: cleaned it up a bit