diff --git a/.gitignore b/.gitignore index b62e513..6147550 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ ./zig-out ./zig-cache +_build +input.txt diff --git a/AdventOfCode2024/AdventOfCode2024.opam b/AdventOfCode2024/AdventOfCode2024.opam new file mode 100644 index 0000000..b296c11 --- /dev/null +++ b/AdventOfCode2024/AdventOfCode2024.opam @@ -0,0 +1,31 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "A short synopsis" +description: "A longer description" +maintainer: ["Maintainer Name"] +authors: ["Author Name"] +license: "LICENSE" +tags: ["topics" "to describe" "your" "project"] +homepage: "https://github.com/username/reponame" +doc: "https://url/to/documentation" +bug-reports: "https://github.com/username/reponame/issues" +depends: [ + "ocaml" + "dune" {>= "3.16"} + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/username/reponame.git" diff --git a/AdventOfCode2024/bin/dune b/AdventOfCode2024/bin/dune new file mode 100644 index 0000000..c35c1b6 --- /dev/null +++ b/AdventOfCode2024/bin/dune @@ -0,0 +1,4 @@ +(executable + (public_name AdventOfCode2024) + (name main) + (libraries AdventOfCode2024)) diff --git a/AdventOfCode2024/bin/main.ml b/AdventOfCode2024/bin/main.ml new file mode 100644 index 0000000..2c19a95 --- /dev/null +++ b/AdventOfCode2024/bin/main.ml @@ -0,0 +1,53 @@ +open Scanf + +let read_lines name = + let ic = open_in name in + let try_read () = + try Some (input_line ic) with End_of_file -> None in + let rec loop acc = match try_read () with + | Some s -> loop (s :: acc) + | None -> close_in ic; List.rev acc in + loop [] + +let handle_line line = + sscanf line "%d %d" (fun num1 num2 -> (num1, num2)) + +let sort_list l = + List.sort compare l + +let build_lists lines = + let rec loop lines l1 l2 = match lines with + | [] -> (l1, l2) + | head :: tail -> + let (num1, num2) = handle_line head in + loop tail (l1 @ [num1]) (l2 @ [num2]) in + loop lines [] [] |> (fun (l1, l2) -> (sort_list l1, sort_list l2)) + +let find_occurances l item = + let rec loop l item acc = match l with + | [] -> acc + | head :: tail -> if head == item then loop tail item (acc + 1) else loop tail item acc in + loop l item 0 + +let () = + print_endline "\nAdvent of Code 2024"; + let lines = read_lines "./bin/input.txt" in + + let rec distance l1 l2 acc = + match (l1, l2) with + | ([], []) -> acc + | (h1 :: t1, h2 :: t2) -> + distance t1 t2 (acc + if h1 > h2 then h1 - h2 else h2 - h1) + | _, _ -> acc in + + let (l1, l2) = build_lists lines in + let part1 = distance l1 l2 0 in + Printf.printf "Part 1: %s\n" (string_of_int part1); + + let rec similarity l1 l2 acc = + match l1 with + | [] -> acc + | head :: tail -> similarity tail l2 (acc + head * find_occurances l2 head) in + + let part2 = similarity l1 l2 0 in + Printf.printf "Part 2: %s\n" (string_of_int part2); diff --git a/AdventOfCode2024/dune-project b/AdventOfCode2024/dune-project new file mode 100644 index 0000000..42dcf4f --- /dev/null +++ b/AdventOfCode2024/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.16) + +(name AdventOfCode2024) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name") + +(maintainers "Maintainer Name") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name AdventOfCode2024) + (synopsis "A short synopsis") + (description "A longer description") + (depends ocaml dune) + (tags + (topics "to describe" your project))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html diff --git a/AdventOfCode2024/lib/dune b/AdventOfCode2024/lib/dune new file mode 100644 index 0000000..306de01 --- /dev/null +++ b/AdventOfCode2024/lib/dune @@ -0,0 +1,2 @@ +(library + (name AdventOfCode2024)) diff --git a/AdventOfCode2024/test/dune b/AdventOfCode2024/test/dune new file mode 100644 index 0000000..4b5a002 --- /dev/null +++ b/AdventOfCode2024/test/dune @@ -0,0 +1,2 @@ +(test + (name test_AdventOfCode2024)) diff --git a/AdventOfCode2024/test/test_AdventOfCode2024.ml b/AdventOfCode2024/test/test_AdventOfCode2024.ml new file mode 100644 index 0000000..e69de29