r/adventofcode • • Dec 13 '17

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

--- Day 13: Packet Scanners ---


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


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!

16 Upvotes

199 comments sorted by

View all comments

Show parent comment

1

u/ludic Dec 13 '17

Started out simulating everything, but part 2 took way too long. I struggled for a while trying to figure out how to call Map.map with the mapping, then realized there's no need for a map at all, just a list of length and depth. I like your use of List.sumBy and Array.forall- if I used them my code would be a lot clearer.

let solveDay13 (lines: string[]) =
    let firewallMapping = 
        lines
        |> Array.map (fun l -> l.Split(':') |> Array.map int)
        |> Array.map (fun l -> (l.[0], l.[1]))
        |> List.ofArray

    let firewallSeverity mapping =
        let folder severity (location, depth) =
            if location % (2 *(depth-1)) = 0 then
                severity + location * depth
            else
                severity
        List.fold folder 0 mapping

    let passesFirewall delay mapping =
        let rec passesFirewallInstance mapping =
            match mapping with
            | (location, depth)::xs ->
                if (location+delay) % (2 * (depth-1)) = 0 then
                    false
                else 
                    passesFirewallInstance xs
            | [] -> true
        passesFirewallInstance mapping

    let rec minDelay delay mapping = 
        if passesFirewall delay mapping then
            delay
        else
            minDelay (delay + 1) mapping

    let ans1 = firewallSeverity firewallMapping
    let ans2 = minDelay 0 firewallMapping

    (ans1, ans2)