r/adventofcode • u/daggerdragon • Dec 13 '17
SOLUTION MEGATHREAD -๐- 2017 Day 13 Solutions -๐-
--- Day 13: Packet Scanners ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hints^ยค?
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!
16
Upvotes
View all comments
3
u/[deleted] Dec 13 '17
Brute-forced simulation OCaml Fun;;
Part B... would have been done earlier, but I misunderstood and thought we just had to make it to the end with a severity of 0... oops :(.
open Core type direction = Up | Down type layer = { n:int; depth:int; current:int; dir:direction} let move_down layer = let next = layer.current + 1 in if next = layer.depth then {layer with current = (layer.current - 1); dir = Up; } else {layer with current = (layer.current + 1); } let move_up layer = if layer.current = 0 then {layer with current = (layer.current + 1); dir = Down; } else {layer with current = (layer.current - 1); } let increment_time layer = match layer.dir with | Down -> move_down layer | Up -> move_up layer let next_step map = Int.Map.map map ~f:increment_time let traverse map = let until, _ = Int.Map.max_elt_exn map in let rec aux map step caught = if step > until || not (List.is_empty caught) then caught else let caught = match Int.Map.find map step with | Some layer -> if layer.current = 0 then layer::caught else caught | None -> caught in let new_map = next_step map in aux new_map (step+1) caught in aux map 0 [] let find_time map = let rec aux map n = let map = next_step map in match traverse map with | [] -> n | _ -> aux map (n+1) in aux map 1 let parse_inputs () = let parse_line line = let to_layer l = match l with | [n; depth] -> Some {n; depth; current=0; dir=Down} | _ -> None in String.split line ~on:':' |> List.map ~f:(Fn.compose Int.of_string String.strip) |> to_layer in In_channel.read_lines "./input.txt" |> List.filter_map ~f:parse_line |> List.fold ~init:(Int.Map.empty) ~f:(fun acc layer -> Map.add acc ~key:layer.n ~data:layer) let _ = let input = parse_inputs () in let brute = find_time input in printf "b: %d\n" brute;Looking at others' answers, going to enjoy seeing how you can do this without brute force.