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