From 15a856261b2c5baa25d5ea135aef710aff4bdc5b Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Thu, 12 Dec 2024 23:25:09 +0000 Subject: [PATCH] Add 2024 day 10 --- README | 4 ++-- 2024/10.ml | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/README b/README index edec381..5e855a0 100644 --- a/README +++ a/README @@ -23,8 +23,8 @@ | 7 | Rust | | 7 | Haskell | +---------------------+ | 8 | Rust | | 8 | Haskell | | 9 | OCaml | | 9 | Haskell | -+---------------------+ | 10 | Haskell | - | 11 | Haskell | +| 10 | OCaml | | 10 | Haskell | ++---------------------+ | 11 | Haskell | | 12 | Rust | | 13 | Haskell | | 14 | Haskell | diff --git a/2024/10.ml b/2024/10.ml new file mode 100755 index 0000000..aa8d4b4 100755 --- /dev/null +++ a/2024/10.ml @@ -1,0 +1,74 @@ +#!/usr/bin/env nix-shell + +(* +#!nix-shell --pure -i ocaml -p ocaml +*) + +let input = + let rec aux () = + try + let parsed_line = + read_line () |> String.to_seq + |> Seq.map (String.make 1) + |> Seq.map int_of_string |> List.of_seq + in + parsed_line :: aux () + with End_of_file -> [] + in + aux () + +let input' = input |> List.map Array.of_list |> Array.of_list + +let is_valid (x, y) = + x >= 0 && y >= 0 && y < Array.length input' && x < Array.length input'.(0) + +let trailheads = + let rec aux x y = function + | [] -> + [] + | [] :: rows -> + aux 0 (y + 1) rows + | (0 :: cols) :: rows -> + (x, y) :: aux (x + 1) y (cols :: rows) + | (_ :: cols) :: rows -> + aux (x + 1) y (cols :: rows) + in + aux 0 0 input + +module RatingMap = Map.Make (struct + type t = int * int + + let compare = compare +end) + +let ratings = + let rec find_paths curr (x, y) = + if curr = 9 then RatingMap.singleton (x, y) 1 + else + let next = curr + 1 in + [(x, y - 1); (x, y + 1); (x - 1, y); (x + 1, y)] + |> List.filter is_valid + |> List.filter (fun (x, y) -> input'.(y).(x) = next) + |> List.map (find_paths next) + |> List.fold_left + (RatingMap.union (fun _ a b -> Some (a + b))) + RatingMap.empty + in + List.map (find_paths 0) trailheads + +let part1 = + List.fold_left (fun acc score -> acc + RatingMap.cardinal score) 0 ratings + +let _ = print_int part1 + +let _ = print_newline () + +let part2 = + List.fold_left + (fun acc score -> + acc + RatingMap.fold (fun _ value acc -> value + acc) score 0 ) + 0 ratings + +let _ = print_int part2 + +let _ = print_newline () -- rgit 0.1.5