Skip to content

Commit

Permalink
2015-13: Remove copied function
Browse files Browse the repository at this point in the history
  • Loading branch information
bcc32 committed Dec 29, 2023
1 parent 219605b commit c91b1e0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 30 deletions.
2 changes: 1 addition & 1 deletion 2015/13/dune
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
30 changes: 1 addition & 29 deletions 2015/13/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c91b1e0

Please sign in to comment.