aoc2021-day07b Clojure/JVM 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:12:38 GMT
COMMAND LINE:
clojure -cp /usr/share/java/data.priority-map.jar aoc2021_day07b.clj-1.clj 0 < aoc2021_day07b-input1000000.txt
TIMED OUT after 1400s
PROGRAM OUTPUT: