changeset 45:ec052bcd3e40

Get started on Day 05 Part 2
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 06 Dec 2023 20:14:02 +0100
parents 070e01565b16
children a035d659f764
files 2023/day05.ml 2023/dune
diffstat 2 files changed, 52 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/2023/day05.ml	Wed Dec 06 20:13:48 2023 +0100
+++ b/2023/day05.ml	Wed Dec 06 20:14:02 2023 +0100
@@ -6,6 +6,9 @@
 type rangemap = { dst_start : int; src_start : int; length : int }
 [@@deriving sexp]
 
+let compare_rangemap { dst_start = dst1; _ } { dst_start = dst2; _ } =
+  Int.compare dst1 dst2
+
 type input = { seeds : int list; maps : (string, rangemap list) Hashtbl.t }
 
 let sexp_of_input { seeds; maps } =
@@ -43,7 +46,7 @@
     let open Angstrom.Let_syntax in
     let%bind mapname = map_headerP in
     let%bind ranges = rangesP in
-    return (mapname, ranges)
+    return (mapname, List.sort ~compare:compare_rangemap ranges)
 
   let seedsP = string "seeds: " *> int_listP <* string "\n\n"
 
@@ -102,9 +105,9 @@
     | [] -> seed
 
   (* optain list of steps from hashmap *)
-  let maps_list maps_hm =
+  let maps_list ?(steps = steps_names) maps_hm =
     let f = Hashtbl.find_exn maps_hm in
-    List.map steps_names ~f
+    List.map steps ~f
 
   (* For all seeds, find resulting location (end number). *)
   let traverse_all { seeds; maps } =
@@ -113,14 +116,51 @@
     results
 
   (* Read, parse, and process input from channel. *)
-  let process ch =
+  let _process ch =
     let input = In_channel.input_all ch in
     let input' = Parse.parse input in
     let result = traverse_all input' in
+    Out_channel.print_endline (Sexp.to_string_hum (sexp_of_input input'));
     Out_channel.print_endline
       (Sexp.to_string_hum (List.sexp_of_t Int.sexp_of_t result));
     Out_channel.printf "Result is %d\n"
       (List.fold ~init:Int.max_value ~f:Int.min result)
 end
 
-let () = Part1.process In_channel.stdin
+module Part2 = struct
+  type range = { from : int; length : int } [@@deriving sexp]
+
+  let _intersect { from = from1; length = length1 }
+      { from = from2; length = length2 } =
+    let f1, t1 = (from1, from1 + length1) in
+    let f2, t2 = (from2, from2 + length2) in
+    let newfrom = Int.max f1 f2 in
+    let newto = Int.min t1 t2 in
+    match (newfrom, newto) with
+    | a, b when a < b -> Some { from = a; length = b - a + 1 }
+    | _ -> None
+
+  let _all_ranges maps =
+    let first = List.hd_exn maps in
+    let last = List.last_exn maps in
+    let range1 = { dst_start = 0; src_start = 0; length = first.dst_start } in
+    let range_last =
+      {
+        src_start = last.dst_start + last.length;
+        dst_start = last.dst_start + last.length;
+        length = Int.max_value - last.dst_start - last.length;
+      }
+    in
+    let maps = if range1.length > 0 then range1 :: maps else maps in
+    if range_last.length > 0 then List.append maps [ range_last ] else maps
+
+  type new_input = {
+    (* seed_ranges : range list; *)
+    _maps : (string, rangemap list) Hashtbl.t;
+  }
+
+  let _convert_new_seeds_to_old ({ maps = _maps; _ } : input) =
+    { (*seed_ranges = convert_list seeds;*) _maps }
+end
+
+let () = Part1._process In_channel.stdin
--- a/2023/dune	Wed Dec 06 20:13:48 2023 +0100
+++ b/2023/dune	Wed Dec 06 20:14:02 2023 +0100
@@ -32,3 +32,10 @@
  (libraries base core angstrom)
  (preprocess
   (pps ppx_let ppx_sexp_conv)))
+
+(executable
+ (name day06)
+ (modules day06)
+ (libraries base core angstrom)
+ (preprocess
+  (pps ppx_let ppx_sexp_conv)))