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/[deleted] Dec 03 '18

OCaml.


let range i j =
  let rec aux n acc = if n < i then acc else aux (n - 1) (n :: acc) in
  aux (j - 1) []

module Coords = struct
  include Tuple.Make (Int) (Int)
  include Tuple.Comparable (Int) (Int)
end

module Claim = struct
  type t = {id: int; x: int; y: int; width: int; height: int}

  let of_string s =
    let id, x, y, width, height =
      Scanf.sscanf s "#%d @ %d,%d: %dx%d" (fun id x y width height ->
          (id, x, y, width, height) )
    in
    {id; x; y; width; height}

  let coords t =
    let xs = range t.x (t.x + t.width) in
    let ys = range t.y (t.y + t.height) in
    List.cartesian_product xs ys

  let stake all_claims t =
    let f = Option.value_map ~default:1 ~f:(fun v -> v + 1) in
    let coords = coords t in
    List.fold coords ~init:all_claims ~f:(fun claims coord ->
        Coords.Map.update claims coord ~f )

  let intact all_claims t  = 
    let f coord = 
      match Coords.Map.find all_claims coord with 
      | None -> false 
      | Some i -> i = 1 
    in
    List.for_all ~f (coords t)
end

let input () =
  In_channel.read_lines "./input.txt" |> List.map ~f:Claim.of_string

let build_claim_map input =
  List.fold input ~init:Coords.Map.empty ~f:Claim.stake

let part_one claim_map = Coords.Map.count ~f:(fun v -> v > 1) claim_map

let part_two claims claim_map =
  List.find ~f:(Claim.intact claim_map) claims
  |> Option.value_map ~default:(-1) ~f:(fun (claim : Claim.t) -> claim.id)

let _ =
  let claims = input () in
  let claim_map = build_claim_map claims in
  printf "overlapping claims: %d" (part_one claim_map);
  printf "\n";
  printf "safe claim: %d" (part_two claims claim_map)