r/adventofcode • • Dec 20 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-

--- Day 20: A Regular Map ---


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 20

Transcript:

My compiler crashed while running today's puzzle because it ran out of ___.


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 00:59:30!

17 Upvotes

153 comments sorted by

View all comments

1

u/alcinos Dec 21 '18

Ocaml

Didn't consider the additional specification that the paths will always branch as soon as possible in the input description, hence a slightly slower solution than those presented by others here. I simply go through the routes, maintaining at each time all the possibles position where we are, and the corresponding depth (this list needs to be made unique).

type direction = |E|S|N|W;;
type sequence =
  | S of direction list
  | And of sequence list
  | Or of sequence list
  | Empty;;

let char_to_dir = function
  | 'E' -> E
  | 'N' -> N
  | 'W' -> W
  | 'S' -> S
  | _ -> failwith "unknown dir code";;


let input_0 = read_line ();;
let input = String.sub input_0 1 ((String.length input_0 )- 2);;

let rec find_level0_or str cur_level cur_pos =
  if cur_pos >= String.length str then [] else (
    match str.[cur_pos] with
  | '|' when cur_level == 0 -> cur_pos::(find_level0_or str cur_level (cur_pos + 1))
  | '(' -> (find_level0_or str (cur_level + 1) (cur_pos + 1))
  | ')' -> (find_level0_or str (cur_level - 1) (cur_pos + 1))
  | _ -> (find_level0_or str (cur_level) (cur_pos + 1)));;

let rec find_level0_and str cur_level cur_pos =
  if cur_pos >= String.length str then [] else (
  match str.[cur_pos] with
  | '(' -> (if cur_level == 0 then [cur_pos] else []) @ (find_level0_and str (cur_level + 1) (cur_pos + 1))
  | ')' -> (if cur_level == 1 then [cur_pos] else []) @ (find_level0_and str (cur_level - 1) (cur_pos + 1))
  | _ -> (find_level0_and str (cur_level) (cur_pos + 1)));;

let rec split str = function
  | [] -> []
  | [a] -> [String.sub str (a+1) (((String.length str) - 1 - a))]
  | a::b::r when a == b -> (String.make 0 'c')::(split str (b::r))
  | a::b::r -> (String.sub str (a+1) ((b - 1 - a)))::(split str (b::r));;

let rec parse = function
  | s when (not(String.contains s '|') && not(String.contains s '(')) ->
    S (List.of_seq (Seq.map char_to_dir (String.to_seq s)))
  | s  when (String.length s >= 1) -> let level0_or = find_level0_or s 0 0 in
    if (List.length level0_or) > 0 then
      Or (List.map (parse) (split s (-1::level0_or)))
    else
      And (List.map (parse) (List.filter (fun s -> String.length s > 0) (split s (-1::(find_level0_and s 0 0)))))
  | _ -> Empty ;;


let hash = Hashtbl.create 55000;;

let update pos value =
  if Hashtbl.mem hash pos then
    Hashtbl.replace hash pos (min value (Hashtbl.find hash pos))
  else
    Hashtbl.add hash pos value;;

let move (x,y) =function
  | E -> (x + 1, y)
  | W -> (x - 1, y)
  | S -> (x, y + 1)
  | N -> (x, y - 1)


let rec get_furthest possibles  = function
  | Empty -> possibles
  | S [] -> possibles
  | S (h::t) -> get_furthest ((List.map (fun (pos, depth) ->
      let new_p = move pos h in
      update new_p (depth + 1); (new_p, depth + 1))
      possibles)) (S t)

  | And [] -> possibles
  | And (h::t) -> get_furthest (get_furthest possibles h) (And t)
  | Or l -> List.sort_uniq compare (List.concat (List.map (get_furthest possibles) l))


let parsed = parse input;;

get_furthest [((0,0),0)] parsed;;
Printf.printf "Part1 %d\n" (Seq.fold_left max 0 (Hashtbl.to_seq_values hash));;
Printf.printf "Part2 %d\n" (Seq.fold_left (fun count value -> count + (if value >= 1000 then 1 else 0)) 0 (Hashtbl.to_seq_values hash));;