diff --git a/2023/09/day09.clj b/2023/09/day09.clj new file mode 100644 index 0000000..0c8bae6 --- /dev/null +++ b/2023/09/day09.clj @@ -0,0 +1,59 @@ +(ns day09 + (:require [clojure.string :as s] + [clojure.edn :as edn])) + +(defn parse-line [line] + (->> (s/split line #" ") + (map edn/read-string))) + +(defn parse-input [input] + (->> (s/split-lines input) + (map parse-line))) + +(defn differences [steps] + (vec (map - (drop 1 steps) steps))) + +(defn all-zeroes? [steps] + (every? zero? steps)) + +(defn expand-line [steps] + (let [diffs (differences steps)] + (if (all-zeroes? diffs) + (last steps) + (let [added (expand-line diffs)] + (+ added (last steps)))))) + +(defn part1 [steps] + (reduce + (map expand-line steps))) + +(defn part2 [steps] + (part1 (map reverse steps))) + +(comment + (= [1 2 3] + (parse-line "1 2 3")) + (= [[1 2 3] [4 5 6]] + (parse-input "1 2 3\n4 5 6")) + (differences [1 2 3]) + (= [3 3 3 3 3] + (differences [0 3 6 9 12 15])) + (= [0 0 0 0] + (differences [3 3 3 3 3])) + (= 18 + (expand-line [0 3 6 9 12 15])) + (= 28 + (expand-line [1 3 6 10 15 21])) + (= 68 + (expand-line [10 13 16 21 30 45])) + + (let [example (parse-input (slurp "example"))] + (and (= 114 (part1 example)) + (= 2 (part2 example))))) + +;; Invoke with clj -X day09/main +;; Or with bb --init day09.clj -e '(day09/main)' +(defn -main [& args] + (let [input (parse-input (slurp "input"))] + (do + (println (str "Part 1: " (part1 input))) + (println (str "Part 2: " (part2 input)))))) diff --git a/2023/09/deps.edn b/2023/09/deps.edn new file mode 100644 index 0000000..a8e645f --- /dev/null +++ b/2023/09/deps.edn @@ -0,0 +1,5 @@ + +{:paths ["."] + :aliases + {:nREPL {:extra-deps + {nrepl/nrepl {:mvn/version "1.0.0"}}}}} diff --git a/2023/09/flake.lock b/2023/09/flake.lock new file mode 100644 index 0000000..ec3d1fa --- /dev/null +++ b/2023/09/flake.lock @@ -0,0 +1,60 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1701680448, + "narHash": "sha256-jyBmBxAIOgSxuViuffo4ah8+NRHfY/9ByozjiLk/rf4=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "6f3d9c7288802bd463f1419c1354a2d8b5ad1d9a", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/2023/09/flake.nix b/2023/09/flake.nix new file mode 100644 index 0000000..f886e39 --- /dev/null +++ b/2023/09/flake.nix @@ -0,0 +1,20 @@ +{ + description = "A flake for pulling in dependencies."; + + inputs = { + + nixpkgs.url = "github:nixos/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + in rec { + devShells.default = with pkgs; mkShell { + buildInputs = [ clojure ]; + }; + } + ); +}