From 01f8d02121c2e8dee088e612abda73dbcbd475c4 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 07 Dec 2024 13:43:18 +0000 Subject: [PATCH] Add 2024 day 2 --- README | 62 ++++++++++++++++++++++++++++++++++++-------------------------- aoc.hs | 1 - flake.lock | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- flake.nix | 12 ++++++++++-- treefmt.nix | 6 ++++++ 2024/02.ml | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2022/06/read.sh | 8 ++++---- 7 files changed, 286 insertions(+), 54 deletions(-) diff --git a/README b/README index cf8d352..e3ffb32 100644 --- a/README +++ a/README @@ -9,34 +9,34 @@ https://adventofcode.com/ -+---------------------+ +---------------------+ -| 2023 | | 2022 | -+---------------------+ +---------------------+ -| Day | Language | | Day | Language | -+----------+----------+ +----------+----------+ -| 1 | Haskell | | 1 | Fortran | -| 2 | Haskell | | 2 | Nix | -| 3 | Haskell | | 3 | Clojure | -| 4 | Haskell | | 4 | Jsonnet | -| 5 | Rust | | 5 | HCL/TF | -| 6 | Haskell | | 6 | Apache2 | -| 7 | Haskell | +---------------------+ -| 8 | Haskell | -| 9 | Haskell | -| 10 | Haskell | -| 11 | Haskell | -| 12 | Rust | -| 13 | Haskell | -| 14 | Haskell | -| 15 | Haskell | -| 16 | Haskell | -| 17 | Rust | -| 18 | Haskell | -| 19 | Haskell | -| 20 | Haskell | -| 21 | Rust | -| 22 | Haskell | -| 23 | Haskell | -| 24 | Haskell | -| 25 | Bash | -+---------------------+ ++---------------------+ +---------------------+ +---------------------+ +| 2024 | | 2023 | | 2022 | ++---------------------+ +---------------------+ +---------------------+ +| Day | Language | | Day | Language | | Day | Language | ++----------+----------+ +----------+----------+ +----------+----------+ +| 1 | Rust | | 1 | Haskell | | 1 | Fortran | +| 2 | OCaml | | 2 | Haskell | | 2 | Nix | ++---------------------+ | 3 | Haskell | | 3 | Clojure | + | 4 | Haskell | | 4 | Jsonnet | + | 5 | Rust | | 5 | HCL/TF | + | 6 | Haskell | | 6 | Apache2 | + | 7 | Haskell | +---------------------+ + | 8 | Haskell | + | 9 | Haskell | + | 10 | Haskell | + | 11 | Haskell | + | 12 | Rust | + | 13 | Haskell | + | 14 | Haskell | + | 15 | Haskell | + | 16 | Haskell | + | 17 | Rust | + | 18 | Haskell | + | 19 | Haskell | + | 20 | Haskell | + | 21 | Rust | + | 22 | Haskell | + | 23 | Haskell | + | 24 | Haskell | + | 25 | Bash | + +---------------------+ diff --git a/aoc.hs b/aoc.hs index 1474b56..8b4d929 100644 --- a/aoc.hs +++ a/aoc.hs @@ -65,4 +65,3 @@ let pairs = zip vertices $ tail vertices sumProd (x1, y1) (x2, y2) = x1 * y2 - x2 * y1 in fromIntegral (abs . sum $ zipWith sumProd vertices (tail vertices)) / 2 - diff --git a/flake.lock b/flake.lock index 02e6b81..2d33bfa 100644 --- a/flake.lock +++ a/flake.lock @@ -1,43 +1,202 @@ { "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "merlin5_1": { + "flake": false, + "locked": { + "lastModified": 1727427299, + "narHash": "sha256-P9+3BPBWrulS/1r03CqMdicFcgEcLK1Gy7pCAcYt3n4=", + "owner": "ocaml", + "repo": "merlin", + "rev": "650a7865bc37a646250f7c52fa6644d9d4a5218b", + "type": "github" + }, + "original": { + "owner": "ocaml", + "ref": "501", + "repo": "merlin", + "type": "github" + } + }, + "merlin5_2": { + "flake": false, + "locked": { + "lastModified": 1727427098, + "narHash": "sha256-ijy7MvHaVOyj99I4M7jqqollbou3ilzcWUctJCuLES4=", + "owner": "ocaml", + "repo": "merlin", + "rev": "0eaccc1b8520d605b1e00685e1c3f8acb5da534c", + "type": "github" + }, + "original": { + "owner": "ocaml", + "ref": "main", + "repo": "merlin", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1702029940, - "narHash": "sha256-qM3Du0perpLesh5hr87mVPZ79McMUKIWUH7EQMh2kWo=", + "lastModified": 1733392399, + "narHash": "sha256-kEsTJTUQfQFIJOcLYFt/RvNxIK653ZkTBIs4DG+cBns=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e9ef8a102c555da4f8f417fe5cf5bd539d8a38b7", + "rev": "d0797a04b81caeae77bcff10a9dde78bc17f5661", "type": "github" }, "original": { - "id": "nixpkgs", - "type": "indirect" + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" } }, "nixpkgs_2": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1727603600, + "narHash": "sha256-bffkUWgbvlDEXPR0QUyHKPuI1FdMmIZvRwIWPP14SYQ=", + "owner": "nix-ocaml", + "repo": "nix-overlays", + "rev": "b5c11f8e03530ab94cd251871bd1a0abcf7bef54", + "type": "github" + }, + "original": { + "owner": "nix-ocaml", + "repo": "nix-overlays", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1727552795, + "narHash": "sha256-IZJVvM+8Jwk8RgWygbfAZ7mnLk0DxGI/2HBDSNxCIio=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "602fb03c3a4aaeb33ea15ae1c921325c593531b1", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "602fb03c3a4aaeb33ea15ae1c921325c593531b1", + "type": "github" + } + }, + "nixpkgs_4": { "locked": { - "lastModified": 1695644571, - "narHash": "sha256-asS9dCCdlt1lPq0DLwkVBbVoEKuEuz+Zi3DG7pR/RxA=", + "lastModified": 1733229606, + "narHash": "sha256-FLYY5M0rpa5C2QAE3CKLYAM6TwbKicdRK6qNrSHlNrE=", "owner": "nixos", "repo": "nixpkgs", - "rev": "6500b4580c2a1f3d0f980d32d285739d8e156d92", + "rev": "566e53c2ad750c84f6d31f9ccb9d00f823165550", "type": "github" }, "original": { "owner": "nixos", - "ref": "nixos-unstable", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", + "type": "github" + } + }, + "ocaml-lsp": { + "inputs": { + "flake-utils": "flake-utils", + "merlin5_1": "merlin5_1", + "merlin5_2": "merlin5_2", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1733424534, + "narHash": "sha256-yNH0n8C0JyGVnoXeFdSHJTXzEBPyxx1jPu7ez1vbNVg=", + "owner": "ocaml", + "repo": "ocaml-lsp", + "rev": "93d9d6554fc127d475c4c27d878552c88d92d43d", + "type": "github" + }, + "original": { + "owner": "ocaml", + "repo": "ocaml-lsp", "type": "github" } }, "root": { "inputs": { "nixpkgs": "nixpkgs", - "systems": "systems", + "ocaml-lsp": "ocaml-lsp", + "systems": "systems_3", "treefmt-nix": "treefmt-nix" } }, "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_3": { "locked": { "lastModified": 1681028828, "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", @@ -53,18 +212,18 @@ }, "treefmt-nix": { "inputs": { - "nixpkgs": "nixpkgs_2" + "nixpkgs": "nixpkgs_4" }, "locked": { - "lastModified": 1702169460, - "narHash": "sha256-BaTrqpMo0IDSmM7fyMFVri5SEtQWLU101w/nn99j9bU=", - "owner": "JordanForks", + "lastModified": 1733440889, + "narHash": "sha256-qKL3vjO+IXFQ0nTinFDqNq/sbbnnS5bMI1y0xX215fU=", + "owner": "numtide", "repo": "treefmt-nix", - "rev": "0b9cf4319069458116e73fc2634a5b7d6316015f", + "rev": "50862ba6a8a0255b87377b9d2d4565e96f29b410", "type": "github" }, "original": { - "owner": "JordanForks", + "owner": "numtide", "repo": "treefmt-nix", "type": "github" } diff --git a/flake.nix b/flake.nix index bd6f8d8..e7350af 100644 --- a/flake.nix +++ a/flake.nix @@ -1,9 +1,11 @@ { inputs = { - treefmt-nix.url = "github:JordanForks/treefmt-nix"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + ocaml-lsp.url = "github:ocaml/ocaml-lsp"; }; - outputs = { self, nixpkgs, systems, treefmt-nix }: + outputs = { self, nixpkgs, ocaml-lsp, systems, treefmt-nix }: let eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system}); treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs ./treefmt.nix); @@ -12,6 +14,12 @@ formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper); checks = eachSystem (pkgs: { formatting = treefmtEval.${pkgs.system}.config.build.check self; + }); + + devShells = eachSystem (pkgs: { + default = pkgs.mkShell { + buildInputs = [ ocaml-lsp.packages.${pkgs.system}.default ]; + }; }); }; } diff --git a/treefmt.nix b/treefmt.nix index a6bf182..eaf485c 100644 --- a/treefmt.nix +++ a/treefmt.nix @@ -11,7 +11,13 @@ fprettify.enable = true; jsonnetfmt.enable = true; shellcheck.enable = true; + ocamlformat.enable = true; }; settings.formatter.shellcheck.options = [ "-s" "bash" ]; + settings.formatter.ocamlformat.options = [ + "--enable-outside-detected-project" + "--profile=ocamlformat" + "--doc-comments-padding=0" # required for shebangs + ]; } diff --git a/2024/02.ml b/2024/02.ml new file mode 100755 index 0000000..d679337 100755 --- /dev/null +++ a/2024/02.ml @@ -1,0 +1,60 @@ +#!/usr/bin/env nix-shell + +(* +#!nix-shell --pure -i ocaml -p ocaml +*) + +let rec read_input acc = + try + read_input + ( (read_line () |> String.split_on_char ' ' |> List.map int_of_string) + :: acc ) + with End_of_file -> acc + +let rec is_ordered = function + | [], _ | [_], _ -> + true + | x :: y :: rest, None -> + is_ordered (y :: rest, Some (x > y)) + | x :: y :: rest, Some gt -> + if (gt && x > y) || ((not gt) && x < y) then + is_ordered (y :: rest, Some gt) + else false + +let rec adjacent_within_bounds = function + | [] | [_] -> + true + | x :: y :: rest -> + let diff = Int.abs (y - x) in + if diff >= 1 && diff <= 3 then adjacent_within_bounds (y :: rest) + else false + +let is_valid x = + let unordered_pairs = is_ordered (x, None) in + let unbounded_adjacent = adjacent_within_bounds x in + unordered_pairs && unbounded_adjacent + +let part1 input = List.filter is_valid input |> List.length + +let part2 input = + let rec remove_at n = function + | [] -> + [] + | h :: t -> + if n = 0 then t else h :: remove_at (n - 1) t + in + let rec bruteforce n x = + if List.length x = n then false + else if is_valid (remove_at n x) then true + else bruteforce (n + 1) x + in + let is_valid' x = is_valid x || bruteforce 0 x in + List.filter is_valid' input |> List.length + +let input = read_input [] + +let () = input |> part1 |> print_int + +let () = print_endline "" + +let () = input |> part2 |> print_int diff --git a/2022/06/read.sh b/2022/06/read.sh index 68ca040..d106bc7 100755 --- a/2022/06/read.sh +++ a/2022/06/read.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash STDIN=$(cat -) -PART1=$(curl --max-redirs 100000 -Ls "http://127.0.0.1:8888/part1/$STDIN" -o /dev/null -w %{url_effective}) -echo $(echo $PART1 | gsed -E 's/.*\/([1]+)/\1/' | tr -d '\n' | wc -c) +PART1=$(curl --max-redirs 100000 -Ls "http://127.0.0.1:8888/part1/$STDIN" -o /dev/null -w '%{url_effective}') +echo "$PART1" | gsed -E 's/.*\/([1]+)/\1/' | tr -d '\n' | wc -c -PART2=$(curl --max-redirs 100000 -Ls "http://127.0.0.1:8888/part2/$STDIN" -o /dev/null -w %{url_effective}) -echo $(echo $PART2 | gsed -E 's/.*\/([1]+)/\1/' | tr -d '\n' | wc -c) +PART2=$(curl --max-redirs 100000 -Ls "http://127.0.0.1:8888/part2/$STDIN" -o /dev/null -w '%{url_effective}') +echo "$PART2" | gsed -E 's/.*\/([1]+)/\1/' | tr -d '\n' | wc -c -- rgit 0.1.5