1
0
Fork 0
This commit is contained in:
Gregory Eremin 2023-01-13 19:54:29 +01:00
parent 18fdc0cb34
commit b0925c89a9
2 changed files with 1038 additions and 0 deletions

1000
inputs/4.txt Normal file

File diff suppressed because it is too large Load Diff

38
ocaml/4.ml Normal file
View File

@ -0,0 +1,38 @@
#use "lib/utils.ml" ;;
let parse_range str =
let nums = String.split_on_char '-' str in
let start = int_of_string (List.nth nums 0) in
let rend = int_of_string (List.nth nums 1) in
(start, rend)
in
let is_full_overlap x y =
let ((x1, x2), (y1, y2)) = (x, y) in
(x1 >= y1 && x2 <= y2) || (y1 >= x1 && y2 <= x2)
in
let is_partial_overlap x y =
let ((x1, x2), (y1, y2)) = (x, y) in
not (x1 > y2 || x2 < y1)
in
let count_overlaps lines fn =
let check_overlap line =
let ranges = String.split_on_char ',' line in
let left = parse_range (List.nth ranges 0) in
let right = parse_range (List.nth ranges 1) in
if fn left right then 1 else 0
in
List.map check_overlap lines
|> List.fold_left (+) 0
in
let contents = read_file "inputs/4.txt" in
let lines = String.split_on_char '\n' contents
|> List.filter (fun l -> String.trim l <> "")
in
let nfull = count_overlaps lines is_full_overlap in
let npartial = count_overlaps lines is_partial_overlap in
Printf.printf "%d full overlaps\n" nfull;
Printf.printf "%d partial overlaps\n" npartial