Skip to content

Commit

Permalink
24/1 in clj
Browse files Browse the repository at this point in the history
  • Loading branch information
enigma committed Dec 10, 2024
1 parent 3939ba1 commit 6551f56
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions clojure/y2024/d01.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bb

(ns y2024.d01
(:require [babashka.cli :as cli]))


(def cli-options {:input {:default "../../inputs/2024/01.input" :type :string}})

(def opts (cli/parse-opts *command-line-args* {:spec cli-options}))

(def input
(->> (slurp (get opts :input))
(re-seq #"\d+")
(map parse-long)
(partition 2)
(apply map vector)))

(defn part1 [[lhs rhs]]
(->> (map - (sort lhs) (sort rhs))
(map abs)
(reduce +)))

(defn part2 [[lhs rhs]]
(let [freqs (frequencies rhs)]
(->> (map #(* % (freqs % 0)) lhs)
(reduce +))))

(prn {:part1 (part1 input)
:part2 (part2 input)})

0 comments on commit 6551f56

Please sign in to comment.