Skip to content

Instantly share code, notes, and snippets.

@sbenhaim
Created December 14, 2015 18:34
Show Gist options
  • Save sbenhaim/0c990f8eab79898e104b to your computer and use it in GitHub Desktop.
Save sbenhaim/0c990f8eab79898e104b to your computer and use it in GitHub Desktop.
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn parse-int [s]
(Integer/parseInt s))
(defn get-coords [^long n [^long sx ^long sy] [^long ex ^long ey]]
(for [x (range sx (inc ex))
y (range sy (inc ey))]
(+ (long x) (* (long n) (long y)))))
(defn parse [inst]
(let [[_ cmd sx sy ex ey] (re-find #"(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)" inst)
sx (parse-int sx)
sy (parse-int sy)
ex (parse-int ex)
ey (parse-int ey)]
[cmd [sx sy] [ex ey]]))
(defn update-board! [board command coords]
(condp = command
"turn on" (doseq [i coords] (aset ^booleans board i true))
"turn off" (doseq [i coords] (aset ^booleans board i false))
"toggle" (doseq [i coords] (aset ^booleans board i ^boolean (not (aget ^booleans board i))))))
(->>
(let [n 1000
board (make-array Boolean/TYPE (* n n))]
(doseq [line (str/split-lines (slurp "a6.txt"))]
(let [[cmd s e] (parse line)]
(update-board! board cmd (get-coords n s e))))
board)
(filter identity)
count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment