Last active
December 5, 2019 01:09
-
-
Save leviroth/60f85577b3d2acfaa4cd0b9762884e70 to your computer and use it in GitHub Desktop.
Advent of code 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open! Core | |
let meets_criteria criteria n = List.for_all criteria ~f:(fun criterion -> criterion n) | |
let solve_criteria criteria (lower, upper) = | |
List.range lower upper |> List.count ~f:(meets_criteria criteria) | |
;; | |
let to_char_list n = Int.to_string n |> String.to_list | |
let to_int_list n = | |
to_char_list n |> List.map ~f:String.of_char |> List.map ~f:Int.of_string | |
;; | |
let has_double n = | |
let rec loop list = | |
match list with | |
| [] | [ _ ] -> false | |
| a :: (b :: _ as rest) -> | |
(match Char.equal a b with | |
| true -> true | |
| false -> loop rest) | |
in | |
to_char_list n |> loop | |
;; | |
let monotonically_increasing n = | |
let rec loop list = | |
match list with | |
| [] | [ _ ] -> true | |
| a :: (b :: _ as rest) -> | |
(match a <= b with | |
| false -> false | |
| true -> loop rest) | |
in | |
to_int_list n |> loop | |
;; | |
let solve_part_1 = solve_criteria [ has_double; monotonically_increasing ] | |
let has_strict_double n = | |
let rec loop list run = | |
match list with | |
| [] -> | |
(match run with | |
| Some (_, 2) -> true | |
| _ -> false) | |
| hd :: tl -> | |
(match run with | |
| None -> loop tl (Some (hd, 1)) | |
| Some (current_element, count) -> | |
(match count, Char.equal current_element hd with | |
| 2, false -> true | |
| _, true -> loop tl (Some (hd, count + 1)) | |
| _, false -> loop tl (Some (hd, 1)))) | |
in | |
let list = to_char_list n in | |
loop list None | |
;; | |
let solve_part_2 = solve_criteria [ has_strict_double; monotonically_increasing ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment