@@ -126,21 +126,14 @@ let solve_echelon m y z =
126126
127127 let norm_of = Array. fold_left (fun a x -> a + (max x 0 )) 0 in
128128
129- let update_best (cur_min , cur_best ) norm x =
130- if norm < cur_min then begin
131- Array. blit x 0 cur_best 0 (Array. length x);
132- Array. map_inplace (fun v -> if v = - 1 then 0 else v) cur_best;
133- (norm, cur_best)
134- end else (cur_min, cur_best) in
135-
136129 let free_variable_in_row x r =
137130 let rec aux i = if x.(i) = - 1 && m.(r).(i) <> 0 then Some i else if i = 0 then None else aux (i - 1 ) in
138131 aux (Array. length x - 1 ) in
139132
140133 let rec aux row best =
141134 let norm = norm_of x in
142- if norm > fst best then best else
143- if row < 0 then update_best best norm x
135+ if norm > best then best else
136+ if row < 0 then min best norm
144137 else match free_variable_in_row x row with
145138 | None -> aux (row - 1 ) best
146139 | Some free_index -> (
@@ -173,7 +166,7 @@ let solve_echelon m y z =
173166 new_best
174167 end
175168 ) in
176- aux (n - 1 ) ( Int. max_int, Array. copy x)
169+ aux (n - 1 ) Int. max_int
177170
178171let solve_part1 machines =
179172 List. map turn_on machines |> List. fold_left (+ ) 0
@@ -184,7 +177,7 @@ let min_joltage (n, _, buttons, joltages) =
184177 let p = echelon b in
185178 let j = mat_vect_prod p joltages in
186179 let m = Array. map (fold_bin (fun a j -> min a joltages.(j)) Int. max_int) buttons in
187- solve_echelon b j m |> fst
180+ solve_echelon b j m
188181
189182let solve_part2 machines =
190183 List. fold_left (fun a m -> a + min_joltage m) 0 machines
0 commit comments