(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)

(def loop-count 1000000)
(def expected-checksum 345000001)

(defn configured-loop-count []
  (try
    (let [line (read-line)
          value (Integer/parseInt (.trim ^String line))]
      (if (pos? value) value loop-count))
    (catch Exception _
      loop-count)))

(defn parse-text []
  (or (first *command-line-args*) "12345"))

(defn parse-loop [limit text]
  (loop [i 0
         acc 1]
    (if (< i limit)
      (let [next (+ acc (Integer/parseInt ^String text))
            bounded (if (> next 1000000000)
                      (- next 1000000000)
                      next)]
        (recur (inc i) bounded))
      acc)))

(let [result (parse-loop (configured-loop-count) (parse-text))]
  (println result)
  (System/exit (if (= result expected-checksum) 0 1)))
