From ee88542095cab7dbced041479a3fe5135beb6291 Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 7 Dec 2024 11:20:56 +0000 Subject: [PATCH] perf(day7): small but easy pruning --- AdventOfCode2024/lib/day_07.ml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/AdventOfCode2024/lib/day_07.ml b/AdventOfCode2024/lib/day_07.ml index 4b75a06..ad15215 100644 --- a/AdventOfCode2024/lib/day_07.ml +++ b/AdventOfCode2024/lib/day_07.ml @@ -37,15 +37,16 @@ let attempt_solve_part2 { equals; numbers } = match numbers with | head :: [] -> [ head ] | head :: tail -> - let plus = List.map (fun i -> i + head) (loop tail) in - let times = List.map (fun i -> i * head) (loop tail) in + let below_solution = List.filter (fun i -> i <= equals) (loop tail) in + let plus = List.map (fun i -> i + head) below_solution in + let times = List.map (fun i -> i * head) below_solution in let concat = List.map (fun i -> let i_string = string_of_int i in let head_string = string_of_int head in int_of_string (i_string ^ head_string)) - (loop tail) + below_solution in plus @ times @ concat | _ -> raise (Invalid_argument "")