diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-01-27 13:43:15 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-01-27 13:43:15 -0800 |
commit | facc9b10d3df43354e684882505fd07a3bc0985f (patch) | |
tree | dc951c96a7c7466d4cb7cb1f670c043fddb6cb7a /day4.ml | |
download | AdventOfCode2017-facc9b10d3df43354e684882505fd07a3bc0985f.tar.gz |
solutions up to day 4
Diffstat (limited to 'day4.ml')
-rw-r--r-- | day4.ml | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/day4.ml b/day4.ml new file mode 100644 index 0000000..e058aec --- /dev/null +++ b/day4.ml @@ -0,0 +1,38 @@ +#use "topfind";; +#thread;; +#require "core";; +#require "stdio";; + +open Core +open Stdio + +let passphrases = In_channel.read_lines "day4.txt" + |> List.map ~f:(fun x -> String.strip x |> String.split ~on:' ') + + +let passphrase_set_lengths p = + let toSet strings = + List.fold ~f:Set.add ~init:(Set.empty (module String)) strings in + List.map ~f:toSet p |> List.map ~f:Set.length + +let passphrase_lengths = List.map ~f:List.length + +let rec has_anagram passphrase = + let string_to_set = + String.fold ~f:Set.add ~init:(Set.empty (module Char)) in + let anagram a b = + Set.equal (string_to_set a) (string_to_set b) in + match passphrase with + | [] -> false + | h::t -> + match List.find t ~f:(anagram h) with + | Some x -> true + | None -> has_anagram t + +let () = + let pairs = List.zip_exn (passphrase_set_lengths passphrases) (passphrase_lengths passphrases) in + let valid_count = List.fold ~f:(fun acc (x, y) -> if x = y then acc+1 else acc) ~init:0 pairs in + Printf.printf "%d\n" valid_count; + + let anagram_count = List.fold ~f:(fun acc x -> if has_anagram x then acc else acc + 1) ~init:0 passphrases in + Printf.printf "%d\n" anagram_count |