day 4 solution

This commit is contained in:
Vladan Popovic 2022-12-04 09:32:45 +01:00
parent 8a9648ff3e
commit f80d5112cf
3 changed files with 1024 additions and 0 deletions

4
bin/d4/dune Normal file
View File

@ -0,0 +1,4 @@
(executable
(public_name d4)
(name main)
(libraries core utils))

1000
bin/d4/input.txt Normal file

File diff suppressed because it is too large Load Diff

20
bin/d4/main.ml Normal file
View File

@ -0,0 +1,20 @@
let is_full (start1, end1) (start2, end2) =
(start1 >= start2 && end1 <= end2) ||
(start2 >= start1 && end2 <= end1)
let is_partial (start1, end1) (start2, end2) =
(start1 >= start2 && start1 <= end2) ||
(start2 >= start1 && start2 <= end1)
open Utils
let split_pair c s = String.split_on_char c s |> pair_of_list
let get_elves = split_pair ','
let get_range range = range |> split_pair '-' |> map_pair int_of_string
let parse row = row |> get_elves |> map_pair get_range
let count_overlapping f l = l |> List.filter (uncurry f) |> List.length
let input = read_file "bin/d4/input.txt" |> List.map parse
let _ = "\n1: " ^ (string_of_int (input |> count_overlapping is_full)) ^
"\n2: " ^ (string_of_int (input |> count_overlapping is_partial)) |> print_endline ;;