diff --git a/2015/13/dune b/2015/13/dune index 3774870..c9ad225 100644 --- a/2015/13/dune +++ b/2015/13/dune @@ -1,6 +1,6 @@ (library (name year2015_day_13_solution) - (libraries angstrom async bignum core core_kernel.composition_infix + (libraries angstrom async bignum core core_kernel.composition_infix euler expect_test_helpers_async re topological_sort yojson) (inline_tests (deps aoc.in)) diff --git a/2015/13/main.ml b/2015/13/main.ml index e795ba0..84ca965 100644 --- a/2015/13/main.ml +++ b/2015/13/main.ml @@ -2,34 +2,6 @@ open! Core open! Async open! Import -(* FIXME: copy *) -let next_permutation array ~compare = - let rec find_first_sorted_from_end pos = - if pos < 0 - then None - else if compare array.(pos) array.(pos + 1) < 0 - then Some pos - else find_first_sorted_from_end (pos - 1) - in - match find_first_sorted_from_end (Array.length array - 2) with - | None -> false - | Some to_increase -> - let rec find_next_largest pos current_min arg = - if pos >= Array.length array - then arg - else if compare array.(pos) current_min < 0 - && compare array.(pos) array.(to_increase) > 0 - then find_next_largest (pos + 1) array.(pos) pos - else find_next_largest (pos + 1) current_min arg - in - let swap_with = - find_next_largest (to_increase + 1) array.(to_increase + 1) (to_increase + 1) - in - Array.swap array to_increase swap_with; - Array.sort array ~pos:(to_increase + 1) ~compare; - true -;; - let parse_line = let parser = let open Angstrom in @@ -78,7 +50,7 @@ let find_highest_gain table ~self = let max_gain = ref Int.min_value in let rec loop () = max_gain := Int.max !max_gain (count_gain set table ~self); - if next_permutation set ~compare:String.compare then loop () + if Euler.Sequences.next_permutation_inplace set ~compare:String.compare then loop () in loop (); !max_gain