aoc2021-day07b Clojure/Babashka program
source code
; SPDX-License-Identifier: MIT
; Copyright (C) 2021 Tito Sacchi <tito@tilde.team>
; WARNING: These solutions were written while I was still learning Clojure and
; should by no means be taken as examples of good programming practice or fast
; implementations.
(ns aoc.2021.07b
(:require
[clojure.string :as str]))
(defn compute-cost [pos x]
(->> pos
(map #(Math/abs (- x %)))
(map #(/ (* % (+ % 1)) 2)) ; Gauss's formula: k*(k+1)/2
(apply +)))
(println (as-> (read-line) pos
(str/split pos #",")
(mapv #(Integer/parseInt %) pos)
; (apply min-key (partial compute-cost pos) (range (apply max pos)))))
(map (partial compute-cost pos) (range (apply max pos)))
(apply min pos)))
notes, command-line, and program output
NOTES:
Linux
Sun, 23 Jan 2022 15:36:35 GMT
COMMAND LINE:
bb -f aoc2021_day07b.clj_babashka-1.clj_babashka 0 < aoc2021_day07b-input1000000.txt
TIMED OUT after 1400s
PROGRAM OUTPUT: