r/adventofcode • • Dec 17 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 17 Solutions -🎄-

--- Day 17: Reservoir Research ---


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 17

Transcript:

All aboard the Easter Bunny HQ monorail, and mind the gap! Next stop: ___


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 01:24:07!

15 Upvotes

105 comments sorted by

View all comments

1

u/alcinos Dec 18 '18

Ocaml

Slow solution today, not sure if I can blame it entirely on the language :/. Runs in ~2min after -O3 compilation.

type content =
  | Empty
  | Clay
  | Wet
  | Water;;

let grid = Array.make_matrix 2000 2000 Empty;;

let minX = ref 2000 and minY = ref 2000 and maxX = ref 0 and maxY = ref 0;;

let rec parse =  function () ->
try
  Scanf.scanf "%c=%d, %c=%d..%d\n" (fun c1 v1 c2 v2 v3 ->
      for i = v2 to v3 do
        let x,y = (if c1=='x' then v1, i else i,v1) in
        grid.(y).(x) <- Clay;
        minX := min (!minX) x;
        minY := min (!minY) y;
        maxX := max (!maxX) x;
        maxY := max (!maxY) y;
      done;
    );
  parse ();
with _ -> ();;


parse ();;
let hash = Hashtbl.create 10000;;

let get_left_right i j =
  let init_left, init_right = if Hashtbl.mem hash (i,j) then Hashtbl.find hash (i,j) else j,j in
  let right = ref init_right and left = ref init_left in
  while grid.(i-1).(!right + 1) != Clay && (grid.(i).(!right) != Empty && grid.(i).(!right) != Wet) do
    incr right;
  done;
  while grid.(i-1).(!left - 1) != Clay && (grid.(i).(!left) != Empty && grid.(i).(!left) != Wet) do
    decr left;
  done;
  Hashtbl.replace hash (i,j) (!left, !right);
  ((!left, !right), !left == !right || (!left != init_left || !right != init_right));;


let rec process i j = if i > (!maxY) then false else begin
    match grid.(i).(j) with
    | Empty -> grid.(i).(j) <- Wet; let _ = process (i+1) j in true;
    | Wet -> process (i+1) j;
    | Clay | Water ->
      let (left, right), new_lr = get_left_right i j in
      let modified = ref false in
      let bounded = grid.(i-1).(right + 1) == Clay && grid.(i-1).(left - 1) == Clay in
      if bounded then begin
        if new_lr then begin
          for x = (left) to (right) do
            if (grid.(i-1).(x) != Water) then modified := true;
            grid.(i-1).(x) <- Water
          done;
        end
      end
      else begin
        if new_lr then begin
          for x = (left) to (right) do
            if (grid.(i-1).(x) != Wet) then modified := true;
            grid.(i-1).(x) <- Wet
          done;
        end;
        if (grid.(i).(right) == Empty || grid.(i).(right) == Wet) then begin
          let r = process (i-1) (right) in modified := (!modified) || r ;
        end;
        if (grid.(i).(left) == Empty || grid.(i).(left) == Wet) then begin
          let r = process (i-1) (left) in modified := (!modified) || r ;
        end;
      end;
      (!modified);
  end;;


let compute_score () =
  let count1 = ref 0 and count2 = ref 0 in
  for i = !minY to !maxY do
    for j = !minX - 1 to !maxX + 1 do
      if grid.(i).(j) == Water || grid.(i).(j) == Wet then
        incr count1;
      if grid.(i).(j) == Water then
        incr count2;
    done;
  done;
  (!count1, !count2);;


let finished = ref false in
while not(!finished) do
  finished := not (process 0 500);
done;;

let count1, count2 = compute_score () in
Printf.printf "Part1 = %d\nPart2 = %d\n" count1 count2;;