Advertisement
luorduz

advent-22-10

Dec 11th, 2022
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clojure 1.22 KB | Source Code | 0 0
  1. (defn perform [cycles [op param]] (let [x (peek cycles)] (
  2.   if (= op "noop")
  3.     (conj cycles x)
  4.     (into cycles [x (+ x (Integer/parseInt param))])                                                                                                            
  5. )))
  6.  
  7. (defn draw [[screen pos] sprite] (
  8.   if (contains? #{-1 0 1} (- (mod pos 40) sprite))
  9.     [(conj screen "#") (inc pos)]
  10.     [(conj screen ".") (inc pos)]
  11. ))
  12.  
  13. (with-open [rdr (clojure.java.io/reader "cycles.in")] (let [                                                                                                            
  14.   lines (->> rdr line-seq (map #(clojure.string/split % #" ")))                                                                                                                
  15.   history (reduce perform [1] lines)
  16. ] (->> history
  17.   (map-indexed vector)
  18.   (filter #(-> % first inc (- 20) (mod 40) zero?))                                                                                                                
  19.   (reduce (fn [total [ix x]] (-> ix inc (* x) (+ total))) 0)
  20.   println)
  21.   (->> history
  22.     (reduce draw [[] 0])
  23.     first
  24.     (partition 40)
  25.     (map clojure.string/join)
  26.     (clojure.string/join "\n")
  27.     println)
  28. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement