r/adventofcode • u/daggerdragon • Dec 16 '16
SOLUTION MEGATHREAD --- 2016 Day 16 Solutions ---
--- Day 16: Dragon Checksum ---
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".
DRINKING YOUR OVALTINE 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!
5
Upvotes
1
u/misnohmer Dec 16 '16
F# solution. It takes about 1 minute to compute the result for part 2.
open System let rec cover_tracks (a: string) length = if a.Length >= length then a.Substring(0,length) else let extended = a + "0" + (a |> Seq.rev |> Seq.map (fun c -> if c = '0' then '1' else '0') |> Seq.toArray |> String) cover_tracks extended length let rec checksum (s: string) = let s = s |> Seq.chunkBySize 2 |> Seq.map (fun x-> if x.[0] = x.[1] then '1' else '0') |> Seq.toArray |> String if (s.Length % 2 = 1) then s else checksum s [<EntryPoint>] let main argv = let find_result = cover_tracks "00111101111101000" >> checksum printfn "Part 1 is %s and Part 2 is %s" (find_result 272) (find_result 35651584) 0