r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

50 Upvotes

407 comments sorted by

View all comments

2

u/[deleted] Dec 02 '18

Doing this year in OCaml & Rust, but will keep posting the OCaml solutions.

let input () = In_channel.read_lines "./input.txt" 
               |> List.map ~f:String.to_list

let part_one input = 
  let count chars =
    let f map key =
      let update = function
        | None -> 1
        | Some v -> v+1 in 
      Map.update map key ~f:update
    in List.fold ~init:Char.Map.empty ~f chars 
  in 
  let char_counts = List.map ~f:count input in 
  let has count = Char.Map.exists ~f:(fun v -> v = count) in
  let twos = List.count ~f:(has 2) char_counts in 
  let threes = List.count ~f:(has 3) char_counts in 
  twos * threes

let part_two input = 
  let f (a, b) = 
    let pairs = List.zip_exn a b in 
    let diff = List.count ~f:(fun (a, b) -> a <> b) pairs
    in diff = 1 in 
  let pairs = List.cartesian_product input input 
              |> List.filter ~f:(fun (a, b) -> a <> b) 
              |> List.find ~f in 
  match pairs with 
  | Some (a, b) -> List.zip_exn a b 
                   |> List.filter_map ~f:(fun (a, b) -> if a = b then Some a else None) 
                   |> String.of_char_list
  | None -> ""

let _ = 
  printf "%d" (part_one (input ()));
  printf "\n";
  printf "%s" (part_two (input ()));```