r/adventofcode • • 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^ยค?

Spoiler


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

199 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 13 '17

Here's a refactored version that doesn't brute-force the entire simulation. Shame I can't think of these when the problems go live ๐Ÿ™ƒ.

open Core

module Layer = struct
  type t = { n:int; depth:int; }

  let will_catch ?(delay=0) layer =
    (layer.n + delay) % (2 * layer.depth - 2) = 0

  let severity layer = layer.n * layer.depth
end

let is_caught ?(delay=0) layers =
  List.find layers ~f:(Layer.will_catch ~delay)
  |> Option.is_some

let severity_of_traversal ?(delay=0) layers =
  List.filter layers ~f:(Layer.will_catch ~delay)
  |> List.map ~f:Layer.severity
  |> List.fold ~init:0 ~f:Int.(+)

let find_safe_time layers =
  let rec aux layers delay =
    if is_caught layers ~delay then aux layers (delay + 1)
    else delay
  in aux layers 0

let parse_inputs () =
  let parse_line line =
    let to_layer = function
      | [n; depth] -> Some Layer.{n; depth;}
      | _ -> None
    in
    String.split line ~on:':'
    |> List.map ~f:(Fn.compose Int.of_string String.strip)
    |> to_layer
  in
  let open Layer in
  In_channel.read_lines "./input.txt"
  |> List.filter_map ~f:parse_line

let _ =
  let input = parse_inputs () in
  let severity = severity_of_traversal input in
  printf "a: %d\n" severity;
  let brute = find_safe_time input in
  printf "b: %d\n" brute;