solved day 4 !
and updated main.ml lol
This commit is contained in:
parent
0a24022629
commit
c61676cf02
|
|
@ -2,5 +2,7 @@ open Aoc
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
Day1.print ();
|
Day1.print ();
|
||||||
Day2.print ()
|
Day2.print ();
|
||||||
|
Day3.print ();
|
||||||
|
Day4.print ()
|
||||||
;;
|
;;
|
||||||
|
|
|
||||||
1000
inputs/day4.tt
Normal file
1000
inputs/day4.tt
Normal file
File diff suppressed because it is too large
Load Diff
40
src/day4.ml
Normal file
40
src/day4.ml
Normal 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)
|
||||||
|
;;
|
||||||
Loading…
Reference in New Issue
Block a user