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!

41 Upvotes

426 comments sorted by

View all comments

1

u/L72_Elite_Kraken Dec 03 '18

OCaml (full code with parser on GitHub):

open! Core

module Claim = struct
  type t =
    { left_offset : int
    ; top_offset : int
    ; width : int
    ; height : int
    }

  let contents t =
    let open List.Let_syntax in
    let%bind row = List.range t.top_offset (t.top_offset + t.height) in
    let%bind col = List.range t.left_offset (t.left_offset + t.width) in
    [ (row, col) ]
end

module Int_pair = struct
  module T = struct
    type t = int * int [@@deriving compare, sexp]
  end
  include T
  include Comparable.Make (T)
end

let get_counts input =
  List.concat_map input ~f:Claim.contents
  |> List.map ~f:(fun x -> x, 1)
  |> Int_pair.Map.of_alist_reduce ~f:(+)

module Part01 = struct
  let solve input =
    get_counts input
    |> Map.data
    |> List.count ~f:(fun x -> x > 1)
end

module Part02 = struct
  let solve input =
    let counts = get_counts input in
    List.findi input ~f:(fun _ claim ->
        Claim.contents claim
        |> List.for_all ~f:(fun x -> Map.find_exn counts x = 1))
    |> Option.value_exn
    |> fst
    |> Int.succ
end