Skip to content

Commit

Permalink
Z not needed for 11
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Dec 11, 2023
1 parent 65ef1db commit 216d150
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/runner/runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
;;

Expand Down
15 changes: 4 additions & 11 deletions lib/year2023/day11.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
;;

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/year2023/day11_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 |}]
;;

0 comments on commit 216d150

Please sign in to comment.