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

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

(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 branch-loop [limit]
  (loop [i 0
         acc 7]
    (if (< i limit)
      (let [bounded (if (> acc 1000000000)
                      (- acc 1000000000)
                      acc)
            branched (if (< i 500000)
                       (+ bounded 3)
                       (+ bounded 7))
            next (if (> branched 1234567)
                   (- branched 1234567)
                   (+ branched 11))]
        (recur (inc i) next))
      acc)))

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