2023 day 9 in Clojure

$ time bb --init day09.clj -m day09
0.04s user 0.02s system 98% cpu 0.058 total
This commit is contained in:
aru 2023-12-09 12:13:43 +01:00
parent e9ece2a757
commit 41777dfb34
4 changed files with 144 additions and 0 deletions

59
2023/09/day09.clj Normal file
View File

@ -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]
(map - (rest 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))))))

5
2023/09/deps.edn Normal file
View File

@ -0,0 +1,5 @@
{:paths ["."]
:aliases
{:nREPL {:extra-deps
{nrepl/nrepl {:mvn/version "1.0.0"}}}}}

60
2023/09/flake.lock Normal file
View File

@ -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
}

20
2023/09/flake.nix Normal file
View File

@ -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 ];
};
}
);
}