Skip to content

Instantly share code, notes, and snippets.

@Rogach
Created December 7, 2018 18:51
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 Rogach/909f47bdd22b5d0ba91a1626d8218481 to your computer and use it in GitHub Desktop.
Save Rogach/909f47bdd22b5d0ba91a1626d8218481 to your computer and use it in GitHub Desktop.
(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