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

2

u/gburri Dec 14 '17 edited Dec 14 '17

Mine :

module AdventOfCode2017.Day14

let hash = Day10.knotHash2Encoding (fun i -> System.Convert.ToString(i, 2).PadLeft(8, '0'))

let buildMatrix (input : string) =
    let mat = Array2D.zeroCreate 128 128
    for i = 0 to 127 do
        input + "-" + (string i) |> hash |> Seq.iteri (fun j c -> mat.[i, j] <- int c - int '0')
    mat

let nbOfUsedSquares (input : string) =
    let mutable i = 0
    buildMatrix input |> Array2D.iter (fun b -> i <- i + b)
    i

let nbOfConnectedRegions (input : string) =
    let m = buildMatrix input

    let rec remove i j =
        if i >= 0 && i < 128 && j >= 0 && j < 128 && m.[i, j] = 1 then
            m.[i, j] <- 0
            1 + remove (i + 1) j * remove (i - 1) j * remove i (j + 1) * remove i (j - 1)
        else
            0

    [ for i in 0 .. 127 do for j in 0 .. 127 -> remove i j ] |> List.sum

Repo : https://github.com/Ummon/AdventOfCode2017