Skip to content

Instantly share code, notes, and snippets.

@onemouth
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onemouth/984976412306544760f7 to your computer and use it in GitHub Desktop.
Save onemouth/984976412306544760f7 to your computer and use it in GitHub Desktop.
POH 5
(defn take-odd [input]
(apply str
(->> (partition 2 2 nil (apply list input))
(mapcat #(take 1 %)))))
(let [line (read-line)]
(println (take-odd line)))
(defn calculate-day-sum [lines]
(let [parts (partition 7 7 nil lines)]
(apply map + parts)))
(let [n-input (Integer. (read-line))
lines (for [x (range n-input)]
(Integer. (read-line)))
day-sum (calculate-day-sum lines)]
(doseq [x day-sum]
(println x)))
(require '[clojure.string :as str])
(defn parse-input-line [input-line]
(->> (str/split input-line #"\s+")
(mapv #(Integer. %))))
(defn empty-table [x y]
(for [i (range y)] (vec (take x (repeat false)))))
(defn enum-point [x1 y1 x2 y2]
(vec (for [x (range x1 (inc x2))
y (range y1 (inc y2))]
[x y])))
(defn table-get [table x y]
(get-in table [(dec y) (dec x)]))
(defn table-set [table x y v]
(assoc-in table [(dec y) (dec x)] v))
(defn sum-points [table flag-table points]
(loop [sum 0 table table flag-table flag-table points points]
(if-let [point (first points)]
(if (= false (apply table-get flag-table point))
(recur (+ sum (apply table-get table point))
table
(table-set flag-table (get point 0) (get point 1) true)
(rest points))
(recur sum table flag-table (rest points)))
sum)))
(let [[x y n] (parse-input-line (read-line))
table (vec (for [i (range y)] (parse-input-line (read-line))))
flag-table (vec (empty-table x y))
points (for [i (range n)] (apply enum-point (parse-input-line (read-line))))
points-cat (mapcat identity points)]
(println (sum-points table flag-table points-cat)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment