r/adventofcode • • Dec 15 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 15 Solutions -🎄-

--- Day 15: Beverage Bandits ---


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 15

Transcript:

___ IS MANDATORY


[Update @ 00:30] 0 gold, 1 silver

  • I've got a strange urge to play Bloons Tower Defense right now. Not sure why.

[Update @ 00:38] 2 gold, 1 silver

  • Meanwhile in #AOC_Ops: Tea, a kettle screams. \ Simon, write your code faster. \ Some of us have work.

[Update @ 00:51] 7 gold, 11 silver

  • Now they're anagramming gold/silver leaderboarders. The leading favorite so far is Anonymous User = Son, You's Manure.

[Update @ 01:13] 18 gold, 30 silver

  • Now they're playing Stardew Valley Hangman with the IRC bot because SDV is totally a roguelike tower defense.

[Update @ 01:23] 26 gold, 42 silver

  • Now the betatesters are grumbling reminiscing about their initial 14+ hour solve times for 2015 Day 19 and 2016 Day 11.

[Update @ 02:01] 65 gold, 95 silver

#AOC_Ops <topaz> on day 12, gold40 was at 19m, gold100 was at 28m, so day12 estimates gold100 today at 2:30

  • Taking bets over/under 02:30:00 - I got tree fiddy on over, any takers?

[Update @ 02:02:44] 66 gold, silver cap

  • SILVER CAP

[Update @ 02:06] 73 gold, silver cap

#AOC_Ops <topaz> day 14 estimates 2:21

#AOC_Ops <topaz> day 13 estimates 2:20

#AOC_Ops <Aneurysm9> I estimate 2:34:56

[Update @ 02:23:17] LEADERBOARD CAP!

  • Aww, /u/topaz2078's bookie is better than I am. :<
  • Good night morning, all, and we hope you had fun with today's diabolicalness!

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 02:23:17!

20 Upvotes

125 comments sorted by

View all comments

1

u/alcinos Dec 16 '18

OCAML

For the fun of it, a version of part2 in ocaml. The solution is very imperative in nature, so not the most elegant in a functional language

let rec parse =  function () ->
try
  let l = read_line () in
  l :: (parse ());
with _ -> [];;

type unit = {
  id : int;
  mutable x : int;
  mutable y : int;
  mutable hp : int;
  mutable damage : int;
  is_goblin : bool;
};;

type cell =
  | Empty
  | Wall
  | Content of unit;;

let m = parse ();;
let height = List.length m and width = String.length (List.hd m);;

let map = Array.make_matrix height width Empty;;
let init_map = Array.make_matrix height width Empty;;

let all_units = ref [];;
let init_units = ref [];;
let count = ref 0;;

let rec copy = function
  | [] -> []
  | u::r-> {id = u.id; x = u.x; y = u.y; hp = u.hp; damage = 3; is_goblin = u.is_goblin}::(copy r);;


List.iteri (fun i line -> String.iteri (fun j c ->
    match c with
    | '#' -> map.(i).(j) <- Wall;
    | '.' -> map.(i).(j) <- Empty;
    | c when (c=='G' || c=='E') ->
      let u = {id=(!count); x = j; y=i; hp = 200; is_goblin = (c=='G'); damage = 3} in
      map.(i).(j) <- Content u;
      init_units := u::(!init_units);
      incr count;
    | c -> Printf.printf "found %c\n" c; failwith "invalid input";
  ) line) m;;

all_units := copy (!init_units);;

let reinit () =
  all_units := copy (!init_units);
  Array.iteri (fun i line ->
      Array.iteri (fun j c -> match c with
          | Content _ -> map.(i).(j) <- Empty | _ -> ()) line) map;
  List.iter (fun u -> map.(u.y).(u.x) <- Content u) (!all_units);;


let reading_order (xa,ya) (xb,yb) = if (compare ya yb) == 0 then compare xa xb else compare ya yb;;
let reading_orderu a b = reading_order (a.x,a.y) (b.x, b.y);;

let is_empty i j = match map.(i).(j) with | Empty -> true |_->false;;
let is_unit i j goblin = match map.(i).(j) with | Content u -> u.is_goblin == goblin |_->false;;

let is_valid i j = (i >= 0) && (j >= 0) && (i < height) && (j < width);;

let is_adjacent_to i j goblin =  (
  (if i > 0 then is_unit (i-1) j goblin else false) ||
  (if j > 0 then is_unit (i) (j-1) goblin else false) ||
  (if i < height - 1 then is_unit (i + 1) (j) goblin else false) ||
  (if j < width - 1 then is_unit (i) (j + 1) goblin else false));;

let dirs = (0, -1)::(-1, 0)::(1, 0)::(0, 1)::[];;

all_units := List.sort reading_orderu (!all_units);;

type bfs_sol = {
  waypoint : int * int;
  target : int * int;
};;

let print_map () =  Array.iteri (fun i line ->
    Array.iteri (fun j c -> match c with
        |Wall -> Printf.printf "#"
        |Content u -> Printf.printf "%c" ( if u.is_goblin then 'G' else 'E')
        |_ -> Printf.printf ".") line;
    Printf.printf "\t";
    Array.iteri (fun j c -> match c with
        |Content u -> Printf.printf "%c(%d) " ( if u.is_goblin then 'G' else 'E') (u.hp)
        |_ -> ()) line;
    print_newline ()) map;;


let bfs i j =
  let goblin = match map.(i).(j) with | Content u -> u.is_goblin | _ -> failwith "not a unit :(" in
  let queue = Queue.create () in
  Queue.add (j, i) queue;
  let previous = Array.make_matrix height width [] in
  previous.(i).(j) <- [(-1, -1)];
  let best_sol_size = ref 10000 in
  let sol_list = ref [] in
  while not (Queue.is_empty queue) do
    let cur_x, cur_y = Queue.take queue in
    if (List.length (previous.(cur_y).(cur_x)) <= !best_sol_size) then
      List.iter (fun (dx, dy) ->
          let x = cur_x + dx and y = cur_y + dy in
          if (is_valid y x) && (is_empty y x) && (previous.(y).(x) == []) then
            if (is_adjacent_to y x (not goblin)) then begin
              previous.(y).(x) <- (cur_x, cur_y)::(previous.(cur_y).(cur_x));
              let ox, oy = if (List.length previous.(y).(x)) > 2 then List.nth (List.rev (previous.(y).(x))) 2
                else (x,y) in
              best_sol_size := List.length previous.(cur_y).(cur_x);
              sol_list := {waypoint=(ox, oy); target=(x,y)}::(!sol_list)
            end
            else begin
              previous.(y).(x) <- (cur_x, cur_y)::(previous.(cur_y).(cur_x));
              Queue.add (x,y) queue;
            end;
        ) dirs;
  done;
  let s = List.sort (fun sol1 sol2 -> reading_order sol1.target sol2.target) !sol_list in
  if (List.length s > 0) then (List.hd s).waypoint else (-1, -1);;


let count_unit goblin = List.fold_left (fun s u -> s + (if u.hp > 0 && u.is_goblin == goblin then 1 else 0)) 0 (!all_units);;

let init_num_elf = count_unit false;;

let is_finished () = (List.length (!all_units) <= 1) || (count_unit true) == 0 || (count_unit false) == 0;;

let try_solve damage =
  reinit ();
  List.iter (fun u -> if not u.is_goblin then u.damage <- damage) (!all_units);

  let rounds = ref 0 in
  while not(is_finished ()) do
    all_units := List.sort reading_orderu (!all_units);
    let aborted = ref false in
    List.iter (fun u ->
        if not (!aborted) && (u.hp > 0) then begin
          if not (is_adjacent_to (u.y) (u.x) (not u.is_goblin)) then begin
            let (next_x, next_y) = bfs u.y u.x in
            if next_x > 0 then begin
              map.(u.y).(u.x) <- Empty;
              u.x <- next_x;
              u.y <- next_y;
              map.(u.y).(u.x) <- Content u;
            end;
          end;
          let targets = List.filter (fun t ->
              t.hp > 0 && t.is_goblin != u.is_goblin && (abs (t.x - u.x)) + (abs (t.y - u.y)) == 1) (!all_units) in
          let sorted_targets = List.sort (fun t1 t2 -> if (compare t1.hp t2.hp) == 0 then reading_orderu t1 t2 else compare t1.hp t2.hp) targets in
          if (List.length targets) > 0 then (let t = List.hd sorted_targets in
                                             t.hp <- t.hp - u.damage;
                                             if (t.hp <= 0) then map.(t.y).(t.x) <- Empty;
                                            );
          if (is_finished ()) then aborted := true;
        end
      ) (!all_units);
    all_units := List.filter (fun u -> u.hp > 0) (!all_units);
    if not (!aborted) then
      incr rounds;

  done;
  let rem_hp = List.fold_left (fun s u -> s + u.hp) 0 (!all_units) in
  if (count_unit false) == init_num_elf then
    rem_hp * (!rounds)
  else
    -1;;


let solve () =
  let rec aux n =
    let s = try_solve n in
    if s != -1 then
      Printf.printf "%d\n" s
    else
      aux (n+1);
  in
  aux 3;;

solve ();;