diff --git a/lib/runner/runner.ml b/lib/runner/runner.ml index 92ff6ec..43c9277 100644 --- a/lib/runner/runner.ml +++ b/lib/runner/runner.ml @@ -20,8 +20,8 @@ let run2023 day part input = | 9, 2 -> Printf.printf "%i\n" @@ Year2023.Day09.part2 input | 10, 1 -> Printf.printf "%i\n" @@ Year2023.Day10.part1 input | 10, 2 -> Printf.printf "%i\n" @@ Year2023.Day10.part2 true input - | 11, 1 -> Stdio.print_endline @@ Year2023.Day11.part1 input - | 11, 2 -> Stdio.print_endline @@ Year2023.Day11.part2 input + | 11, 1 -> Printf.printf "%i\n" @@ Year2023.Day11.part1 input + | 11, 2 -> Printf.printf "%i\n" @@ Year2023.Day11.part2 input | day, part -> failwith @@ Printf.sprintf "Unknown day %i and part %i" day part ;; diff --git a/lib/year2023/day11.ml b/lib/year2023/day11.ml index a93a671..561c449 100644 --- a/lib/year2023/day11.ml +++ b/lib/year2023/day11.ml @@ -9,10 +9,9 @@ let parse input = input |> String.split_lines |> List.map ~f:String.to_list let expand amount galaxy = let build_counts l = - let open Z in l |> List.mapi ~f:(fun i line -> - i, if List.for_all line ~f:(Char.equal '.') then ~$amount else ~$1) + i, if List.for_all line ~f:(Char.equal '.') then amount else 1) |> IntMap.of_alist_exn in build_counts (List.transpose_exn galaxy), build_counts galaxy @@ -35,13 +34,8 @@ let pairs grid = let range a b = if a < b then List.range a b else List.range b a let distance x_counts y_counts ((x1, y1), (x2, y2)) = - let open Z in - let x_dist = - range x1 x2 |> List.fold ~init:~$0 ~f:(fun sum x -> Map.find_exn x_counts x + sum) - in - let y_dist = - range y1 y2 |> List.fold ~init:~$0 ~f:(fun sum y -> Map.find_exn y_counts y + sum) - in + let x_dist = range x1 x2 |> List.sum (module Int) ~f:(Map.find_exn x_counts) in + let y_dist = range y1 y2 |> List.sum (module Int) ~f:(Map.find_exn y_counts) in x_dist + y_dist ;; @@ -52,8 +46,7 @@ let solve amount input = |> to_grid |> pairs |> List.map ~f:(distance x_counts y_counts) - |> List.fold ~init:Z.zero ~f:Z.add - |> Z.to_string + |> Util.List.sum_int ;; let part1 input = solve 2 input diff --git a/test/year2023/day11_test.ml b/test/year2023/day11_test.ml index e5a3bb2..2d86b05 100644 --- a/test/year2023/day11_test.ml +++ b/test/year2023/day11_test.ml @@ -13,11 +13,11 @@ let input = ;; let%expect_test "2023 day11 part1" = - Stdio.print_endline @@ Year2023.Day11.part1 input; + Printf.printf "%i" @@ Year2023.Day11.part1 input; [%expect {| 374 |}] ;; let%expect_test "2023 day11 part2" = - Stdio.print_endline @@ Year2023.Day11.part2 input; + Printf.printf "%i" @@ Year2023.Day11.part2 input; [%expect {| 82000210 |}] ;;