r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

39 Upvotes

426 comments sorted by

View all comments

2

u/wegry Dec 08 '18

F#

module AdventOfCode.Day3

open System.IO
open System

type Claim = {
    claimNumber: int32
    fromLeft: int32
    fromTop: int32
    width: int32
    height: int32
}
type Claim with
    static member from claimNumber fromLeft fromTop width height =
       { Claim.claimNumber = claimNumber
         fromLeft = fromLeft
         fromTop = fromTop
         width = width
         height = height }

module private Internal =
    let claimToPoints claim =
        seq {
            for i in 0..1000  do
                for j in 0..1000  do
                    if ( i >= claim.fromLeft && i < claim.fromLeft + claim.width
                    && j >= claim.fromTop && j < claim.fromTop + claim.height )
                        then yield (i, j)
        } |> Set.ofSeq

    let parseRow row =
        let (claimNumber, fromLeft, fromTop, width, height) = Utils.sscanf "#%d @ %d,%d: %dx%d" row

        Claim.from claimNumber fromLeft fromTop width height

    let generateClaims () =
        File.ReadAllLines("day-3.txt")
        |> Seq.map parseRow

    let countOverlap claims =
        claims
        |> Seq.collect Set.toSeq
        |> Seq.countBy id
        |> Seq.filter (fun (_, count) -> count > 1)
        |> Seq.length

    let areOverlapping (a: Claim) (b: Claim) =
        let topLeft a = a.height + a.fromTop, a.fromLeft
        let bottomRight a = a.height, a.width + a.fromLeft

        let pull margin breadth x =
            [for i in (margin x)..(margin x + breadth x) do yield i] |> Set.ofSeq
        let overlappingX a b =
            let margin x = x.fromLeft
            let breadth x = x.width
            let ranger = pull margin breadth

            Set.intersect (ranger a) (ranger b)
            |> (Set.isEmpty >> not)

        let overlappingY a b =
            let margin x = x.fromTop
            let breadth x = x.height
            let ranger = pull margin breadth

            Set.intersect (ranger a) (ranger b)
            |> (Set.isEmpty >> not)

        overlappingY a b && overlappingX a b

    let testClaim =
        { Claim.fromLeft = 0
          fromTop = 0
          height = 5
          width = 5
          claimNumber = 1 }

    let countNonOverlap (nonOverlapped, overlapped) (current: Claim) =
       let notYetOverlapped = Set.filter (areOverlapping current) nonOverlapped
       let alreadyOverlapped = Set.exists (areOverlapping current) overlapped

       match Set.count notYetOverlapped, alreadyOverlapped with
       | (0, false) -> Set.add current nonOverlapped, overlapped
       | (_, _) ->
            Set.difference nonOverlapped notYetOverlapped,
            overlapped
            |> Set.add current
            |> Set.union notYetOverlapped

open Internal

let part1 () =
    generateClaims()
    |> Seq.map claimToPoints
    |> countOverlap

let part2 () =
    generateClaims()
    |> Seq.fold countNonOverlap (Set.empty, Set.empty)
    |> fst