diff --git a/lib/day5.ml b/lib/day5.ml index 8c1500a..48f35a4 100644 --- a/lib/day5.ml +++ b/lib/day5.ml @@ -1,3 +1,4 @@ +let compare_ranges fr sr = if fst fr > fst sr then 1 else -1 let split_empty_line lines = let rec split acc = function | [] -> (List.rev acc), [] @@ -33,8 +34,8 @@ let update_range (x, y) ranges = else if u1 > t2 then (u1, u2) else if u2 < t1 then (u1, u2) else if t1 <= u1 && t2 >= u2 then (0,0) - else if t1 >= u1 && u1 <= t2 then (u1, t1 - 1) - else if t1 <= u1 && t2 >= u1 then (t2 + 1, u2) + else if t1 >= u1 && u2 <= t2 then (u1, t1 - 1) + else if t1 <= u1 && u2 >= t2 then (t2 + 1, u2) else (u1 ,u2) ) (x, y) ranges @@ -43,9 +44,9 @@ let rec update_ranges to_process updated_ranges = | [] -> updated_ranges | (x, y) :: xs -> let (u1, u2) = update_range (x, y) xs in - Printf.printf "Updating range: (%i,%i) to (%i, %i)\n" x y u1 u2; - update_ranges xs ((u1, u2) :: updated_ranges) - + let removed = List.filter (fun (t1, t2) -> not (t1 <= u2 && t2 >= u1 && u2 <= t2)) xs in + update_ranges removed ((u1, u2) :: updated_ranges) + let solve_part2 inputs = let (first, _) = split_empty_line inputs in let ranges = create_ranges first in