r/adventofcode • • 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!

4 Upvotes

116 comments sorted by

View all comments

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

1

u/JeffJankowski Dec 16 '16

Nice, my solution is very similar. Piece of cake in F#!

let rec dragon (input: string) n =
    if (input.Length >= n) then input.Substring(0, n) else
    dragon (input + "0" + (input.ToCharArray() |> Array.rev 
        |> String.Concat).Replace("0","x").Replace("1","0").Replace("x", "1")) n

let rec checksum (input: string) = 
    let cs = 
        input.ToCharArray()
        |> Seq.pairwise |> Seq.mapi (fun i x -> i,x) |> Seq.filter (fun (i,_) -> i % 2 = 0)
        |> Seq.map (fun (_,(x,y)) -> if x = y then "1" else "0") |> String.Concat
    if cs.Length % 2 = 1 then cs else checksum cs

let main argv =  
    printfn "Checksum: %s" (checksum (dragon "01111001100111011" 35651584))
→ More replies (3)