solved day 4 !

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

View File

@ -2,5 +2,7 @@ open Aoc
let () =
Day1.print ();
Day2.print ()
Day2.print ();
Day3.print ();
Day4.print ()
;;

1000
inputs/day4.tt Normal file

File diff suppressed because it is too large Load Diff

40
src/day4.ml Normal file
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)
;;