Created
December 14, 2015 18:34
-
-
Save sbenhaim/0c990f8eab79898e104b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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