r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


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

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

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


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 at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/fftk2 Dec 15 '18 edited Dec 15 '18

OCAML

open Stdio
open Base

(* Advent of Code puzzle #6 part 1 and 2 *)
let input_file = "input.txt"

let first (a,_) = a
let second (_,b) = b

let parse_line line =
  let matches =
    Re.exec (Re.Posix.compile_pat "-?([0-9]+), -?([0-9]+)") line
  in
  (Int.of_string (Re.get matches 1), Int.of_string (Re.get matches 2))

let coords = In_channel.read_lines input_file
             |> List.map ~f:parse_line

let coord_map = coords |> List.mapi ~f:(fun i v -> (v,i))

let man_distance (x1,y1) (x2,y2) = abs (x1 - x2) + abs (y1 -y2)

let top_left cds =
  let x = cds |> List.map ~f:(fun (a,_) -> a) |>  List.min_elt ~compare:compare in
  let y = cds |> List.map ~f:(fun (_,b) -> b) |>  List.min_elt ~compare:compare in
  let x' = match x with
    | Some n -> n
    | None -> 0 in
  let y' = match y with
    | Some n -> n
    | None -> 0 in
  (x',y')

let bottom_right cds =
  let x = cds |> List.map ~f:(fun (a,_) -> a) |> List.max_elt ~compare:compare in
  let y = cds |> List.map ~f:(fun (_,b) -> b) |> List.max_elt ~compare: compare in
  let x' = match x with
    | Some n -> n
    | None -> 0 in
  let y' = match y with
    | Some n -> n
    | None -> 0 in
  (x',y')

let grid cs =
  let tl = top_left cs in
  let br = bottom_right cs in
  let xs = List.range (first tl) (first br) in
  let ys = List.range (second tl) (second br) in
  List.cartesian_product xs ys


let assign_label ls = let fst = List.hd_exn ls in
  match (List.length fst) with
  | 1 ->  (second (first (List.hd_exn fst)))
  | _ -> (-1)

let shortest_man_distance ls = List.sort ~compare:(fun a b -> compare (second a) (second b)) ls
                               |> List.group ~break:(fun a b -> (not ((second a) = (second b))))


let part1 =
  let labeled = List.map ~f:(fun x -> (x, List.map ~f:(fun y -> (y, man_distance x (first y))) coord_map)) (grid coords)
                |> List.map ~f:(fun (a,b) -> (a, shortest_man_distance b))
                |> List.map ~f:(fun (a,b) -> (a, assign_label b)) in
  
  let border_labels = labeled
                      |> List.filter ~f:(fun ((x,y), _) ->
                          let (bl,bt) = top_left coords in
                          let (br,bb) = bottom_right coords in
                          (x = bl || x = br || y = bt || y = bb))
                      |> List.dedup_and_sort ~compare:(fun (_, label) ((_, label'))-> compare label label' )
                      |> List.map ~f:(fun (_,l) -> l) in
  labeled
  |> List.filter ~f:(fun (_,label) -> if List.exists ~f:(fun l -> l = label) border_labels then false else true)
  |> List.sort ~compare:(fun ((_,_), label) ((_,_), label') -> compare label label')
  |> List.group ~break:(fun ((_,_), label) ((_,_), label') -> (not (label = label')))
  |> List.map ~f:(fun x -> (second (List.hd_exn x)), (List.length x))
  |> List.sort ~compare:(fun (_,v) (_,v') -> compare v v')
  |> List.last_exn
  |> second


let part2 = List.map ~f:(fun x -> (x, List.map ~f:(fun y -> (y, man_distance x (first y))) coord_map)) (grid coords)
            |> List.map ~f:(fun (coord, pts) -> (coord, List.fold ~init:0 ~f:(fun accum (_,v) -> accum + v) pts))
            |> List.filter ~f:(fun (_,v) -> v < 10000)
            |> List.length
         
let () =
  printf "%d\n" part1;
  printf "%d\n" part2