From 4daf41bd69e01f137be90881ec419b3c045a2ec5 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sun, 5 Feb 2023 19:09:06 +0100 Subject: Try clojure for day 1 and test transducers --- AoC2022/aocclj/.gitignore | 13 +++++++++++++ AoC2022/aocclj/project.clj | 10 ++++++++++ AoC2022/aocclj/src/aocclj/core.clj | 8 ++++++++ AoC2022/aocclj/src/aocclj/day01.clj | 18 ++++++++++++++++++ AoC2022/aocclj/test/aocclj/core_test.clj | 7 +++++++ AoC2022/aocclj/test/aocclj/day01_test.clj | 10 ++++++++++ 6 files changed, 66 insertions(+) create mode 100644 AoC2022/aocclj/.gitignore create mode 100644 AoC2022/aocclj/project.clj create mode 100644 AoC2022/aocclj/src/aocclj/core.clj create mode 100644 AoC2022/aocclj/src/aocclj/day01.clj create mode 100644 AoC2022/aocclj/test/aocclj/core_test.clj create mode 100644 AoC2022/aocclj/test/aocclj/day01_test.clj (limited to 'AoC2022') diff --git a/AoC2022/aocclj/.gitignore b/AoC2022/aocclj/.gitignore new file mode 100644 index 0000000..d956ab0 --- /dev/null +++ b/AoC2022/aocclj/.gitignore @@ -0,0 +1,13 @@ +/target +/classes +/checkouts +profiles.clj +pom.xml +pom.xml.asc +*.jar +*.class +/.lein-* +/.nrepl-port +/.prepl-port +.hgignore +.hg/ diff --git a/AoC2022/aocclj/project.clj b/AoC2022/aocclj/project.clj new file mode 100644 index 0000000..d17ed4c --- /dev/null +++ b/AoC2022/aocclj/project.clj @@ -0,0 +1,10 @@ +(defproject aocclj "0.1.0-SNAPSHOT" + :description "FIXME: write description" + :url "http://example.com/FIXME" + :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" + :url "https://www.eclipse.org/legal/epl-2.0/"} + :dependencies [[org.clojure/clojure "1.11.1"]] + :main ^:skip-aot aocclj.core + :target-path "target/%s" + :profiles {:uberjar {:aot :all + :jvm-opts ["-Dclojure.compiler.direct-linking=true"]}}) diff --git a/AoC2022/aocclj/src/aocclj/core.clj b/AoC2022/aocclj/src/aocclj/core.clj new file mode 100644 index 0000000..35a8acb --- /dev/null +++ b/AoC2022/aocclj/src/aocclj/core.clj @@ -0,0 +1,8 @@ +(ns aocclj.core + (:gen-class) + (:require [clojure.string :as str])) + +(defn -main + "I don't do a whole lot ... yet." + [& args] + (println "Hello, World!")) diff --git a/AoC2022/aocclj/src/aocclj/day01.clj b/AoC2022/aocclj/src/aocclj/day01.clj new file mode 100644 index 0000000..8fca2e6 --- /dev/null +++ b/AoC2022/aocclj/src/aocclj/day01.clj @@ -0,0 +1,18 @@ +(ns aocclj.day01 + (:require [clojure.string :as str])) + +(defn preprocess [input] + (transduce + (comp + (map parse-long) + (partition-by nil?) + (take-nth 2) + (map (partial reduce +))) + conj + input)) + +(defn part1 [input] + (apply max (preprocess input))) + +(defn part2 [input] + (->> input preprocess sort (take 3) (apply +))) diff --git a/AoC2022/aocclj/test/aocclj/core_test.clj b/AoC2022/aocclj/test/aocclj/core_test.clj new file mode 100644 index 0000000..15216e7 --- /dev/null +++ b/AoC2022/aocclj/test/aocclj/core_test.clj @@ -0,0 +1,7 @@ +(ns aocclj.core-test + (:require [clojure.test :refer :all] + [aocclj.core :refer :all])) + +(deftest a-test + (testing "FIXME, I fail." + (is (= 1 1)))) diff --git a/AoC2022/aocclj/test/aocclj/day01_test.clj b/AoC2022/aocclj/test/aocclj/day01_test.clj new file mode 100644 index 0000000..ca1262c --- /dev/null +++ b/AoC2022/aocclj/test/aocclj/day01_test.clj @@ -0,0 +1,10 @@ +(ns aocclj.day01-test + (:require [aocclj.day01 :as sut] + [clojure.test :as t] + [clojure.string :as str])) + +(t/deftest solutions + (let [input (str/split-lines (slurp "../01/input"))] + (t/are [expected function] (= expected (function input)) + 75622 sut/part1 + 213159 sut/part2))) -- cgit v1.2.3