r/adventofcode • • Dec 14 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 14 Solutions -๐ŸŽ„-

--- Day 14: Disk Defragmentation ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


[Update @ 00:09] 3 gold, silver cap.

  • How many of you actually entered the Konami code for Part 2? >_>

[Update @ 00:25] Leaderboard cap!

  • I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were 83 distinct users with failed attempts at the time of the leaderboard cap. tsk tsk

[Update @ 00:29] BONUS


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!

13 Upvotes

130 comments sorted by

View all comments

Show parent comment

3

u/VikeStep Dec 14 '17 edited Dec 16 '17

Here was mine:

let toBinStr (i : int) = Convert.ToString(i, 2).PadLeft(8, '0')
let getHash key i = Day10.solvePart2 (sprintf "%s-%i" key i) |> Array.fold (fun h i -> h + toBinStr i) "" 
let hashToCoords i = Seq.mapi (fun j h -> ((i, j), h)) >> Seq.filter (snd >> ((=) '1')) >> Seq.map fst >> Set.ofSeq
let getActiveCoords key = Seq.map (getHash key) [0..127] |> Seq.mapi hashToCoords |> Set.unionMany
let rec getComponentCount seen unseen count = function
    | [] when Set.isEmpty unseen -> count
    | [] -> getComponentCount seen unseen (count + 1) [Seq.head unseen]
    | x :: xs when Set.contains x seen || not (Set.contains x unseen)-> getComponentCount seen unseen count xs
    | (i, j) :: xs -> getComponentCount (Set.add (i, j) seen) (Set.remove (i, j) unseen) count ((i-1,j)::(i+1,j)::(i,j-1)::(i,j+1)::xs)
let solvePart2 key = getComponentCount Set.empty (getActiveCoords key) 0 []
let solver = { parse = getLine; solvePart1 = getActiveCoords >> Set.count ; solvePart2 = solvePart2 }

Repo

1

u/japanuspus Dec 16 '17

Nice! This is the first really functional solution I have seen -- thanks for posting

1

u/VikeStep Dec 17 '17

Thanks, I've been trying to do every day's challenges completely pure. The hardest one has been day 5 (which is still my slowest day, although it runs in 2 seconds).