From 0a24022629ded013c30be84fadb8958f307b1401 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 22 Sep 2023 15:53:13 -0700 Subject: [PATCH] replace parse_line with CC map_same and take_drop thank u https://doc.sherlocode.com --- src/day3.ml | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/day3.ml b/src/day3.ml index 0efa761..7b68b8d 100644 --- a/src/day3.ml +++ b/src/day3.ml @@ -10,22 +10,16 @@ let priority char = | _ -> failwith @@ Printf.sprintf "'%c' is not a valid item" char ;; - -let parse_line line = - let half_len = String.length line / 2 in - let first_half = String.sub line 0 half_len in - let second_half = String.sub line half_len half_len in - set_of_string first_half, set_of_string second_half -;; - let part1 lines = List.fold_left (fun acc line -> - let first, second = parse_line line in + let half_len = String.length line / 2 in + let first, second = Pair.map_same set_of_string (String.take_drop half_len line) in let item = match CharSet.elements @@ CharSet.inter first second with | [ item ] -> item - | _ -> failwith "sad" + | _ -> + failwith "first and second half of each line should only have 1 intersection" in acc + priority item) 0 @@ -33,9 +27,9 @@ let part1 lines = ;; (* -x thank you https://doc.sherlocode.com/ for helping me find Containers and its -x List.chunks and List.reduce functions -x*) + shout out https://doc.sherlocode.com/ for helping me find Containers and its + List.chunks and List.reduce functions +*) let part2 lines = let groups = List.chunks 3 (List.map set_of_string lines) in List.fold_left @@ -43,7 +37,7 @@ let part2 lines = let item = match CharSet.elements @@ List.reduce_exn CharSet.inter group with | [ item ] -> item - | _ -> failwith "sad" + | _ -> failwith "each group of 3 lines should only have 1 intersection" in acc + priority item) 0