solved day 4 !

and updated main.ml lol
This commit is contained in:
ryan committed 2023-09-22 16:59:03 -07:00
1 parent 0a24022629
commit c61676cf02
3 files changed
+1043 -1

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
open Containers
let lines = Utils.lines_of_input 4
let parse_line =
function%pcre
| {|(?<a>\d+)-(?<b>\d+),(?<c>\d+)-(?<d>\d+)|} ->
(int_of_string a, int_of_string b), (int_of_string c, int_of_string d)
| line -> failwith @@ Printf.sprintf "cannot parse line '%s'" line
;;
let count_overlaps is_overlapping =
lines
|> List.fold_left
(fun acc line ->
match is_overlapping @@ parse_line line with
| true -> acc + 1
| false -> acc)
0
;;
let part1 =
count_overlaps (fun pairs ->
match pairs with
| (l1, r1), (l2, r2) when (l1 <= l2 && r1 >= r2) || (l2 <= l1 && r2 >= r1) -> true
| _ -> false)
;;
let part2 =
count_overlaps (fun pairs ->
match pairs with
| (l1, r1), (l2, r2) when r1 < l2 || r2 < l1 -> false
| _ -> true)
;;
let print () =
Printf.(
printf "Day 4.1: %d\n" part1;
printf "Day 4.2: %d\n" part2)
;;