Created
December 7, 2018 18:51
-
-
Save Rogach/909f47bdd22b5d0ba91a1626d8218481 to your computer and use it in GitHub Desktop.
This file contains 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
(def W 1000) | |
(def input (clojure.string/split-lines (slurp "d06.txt"))) | |
(let [lights (boolean-array (* W W))] | |
(doseq [s input] | |
(let [ m (re-matches #"(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)" s) | |
command (get m 1) | |
x1 (Integer/parseInt (get m 2)) | |
y1 (Integer/parseInt (get m 3)) | |
x2 (Integer/parseInt (get m 4)) | |
y2 (Integer/parseInt (get m 5)) ] | |
(cond | |
(= command "turn on") | |
(doseq [ x (range x1 (inc x2)) | |
y (range y1 (inc y2)) | |
:let [i (+ (* y W) x)] ] | |
(aset lights i true)) | |
(= command "turn off") | |
(doseq [ x (range x1 (inc x2)) | |
y (range y1 (inc y2)) | |
:let [i (+ (* y W) x)] ] | |
(aset lights i false)) | |
(= command "toggle") | |
(doseq [ x (range x1 (inc x2)) | |
y (range y1 (inc y2)) | |
:let [i (+ (* y W) x)] ] | |
(aset lights i (not (aget lights i)))) | |
))) | |
(printf "%d\n" (count (filter identity lights)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment