diff --git a/bin/day2405.ml b/bin/day2405.ml index d277594..103d8ef 100644 --- a/bin/day2405.ml +++ b/bin/day2405.ml @@ -39,13 +39,12 @@ let rec is_page_order_valid m pages = encountering an empty line. Returns a pair [(rule_map, tail)]. [tail] starts the line after the empty line. *) let parse_rules = - let re = Str.regexp_string "|" in let m = Hashtbl.create 17 in let rec impl = function | "" :: t -> (m, t) | [] -> failwith "parse_rules.impl" | h :: t -> ( - match List.map int_of_string (Str.split re h) with + match Aoc.ints_of_string ~sep:"|" h with | [ a; b ] -> add_rule a b m; impl t @@ -55,10 +54,9 @@ let parse_rules = (** [parse_page_orders lst] parses a list of page orders. *) let parse_page_orders = - let re = Str.regexp_string "," in let rec impl acc = function | [] -> acc - | h :: t -> impl (List.map int_of_string (Str.split re h) :: acc) t + | h :: t -> impl (Aoc.ints_of_string ~sep:"," h :: acc) t in impl [] diff --git a/lib/aoc.ml b/lib/aoc.ml index 1e87e34..1c0dbb0 100644 --- a/lib/aoc.ml +++ b/lib/aoc.ml @@ -1,4 +1,6 @@ -let ints_of_string s = List.map int_of_string (Str.split (Str.regexp " +") s) +let ints_of_string ?(sep = " ") s = + List.map int_of_string (Str.split (Str.regexp sep) s) + let distance1 a b = abs (a - b) let strings_of_file fname = diff --git a/lib/aoc.mli b/lib/aoc.mli index 04f2a41..473de53 100644 --- a/lib/aoc.mli +++ b/lib/aoc.mli @@ -1,6 +1,6 @@ -val ints_of_string : string -> int list -(** [nums_from_string s] takes a string of space separated integers and gives - back a list of the integers. *) +val ints_of_string : ?sep:string -> string -> int list +(** [nums_from_string ?sep s] takes a string of integers separated by [sep] and + gives back a list of the integers. By default [sep] is " " *) val distance1 : int -> int -> int (** [distance1 a b] returns the absolute difference between [a] and [b]. *)