Created
December 14, 2015 04:37
-
-
Save sbenhaim/19f2d3c61d24a50396bb 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
(defn parse-int [s] | |
(Integer/parseInt s)) | |
(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] | |
(let [f (condp = command | |
"turn on" (fn [b c] (assoc-in b c true)) | |
"turn off" (fn [b c] (assoc-in b c false)) | |
"toggle" (fn [b c] (update-in b c not)))] | |
(reduce f board coords))) | |
(->> (let [n 1000 | |
board (vec (repeat n (vec (repeat n false))))] | |
(reduce (fn [b line] (let [[cmd s e] (parse line)] | |
(update-board b cmd (get-coords s e)))) | |
board | |
(str/split-lines (slurp "a6.txt")))) | |
flatten | |
(filter identity) | |
count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment