Created
December 16, 2015 03:35
-
-
Save sbenhaim/3306db97adf516b82bc1 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
(ns a6 | |
(:require [clojure.string :as str])) | |
(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] | |
(let [f (condp = command | |
"turn on" (fn [b i] (update b i inc)) | |
"turn off" (fn [b i] (update b i (fn [i] (if (zero? i) 0 (dec i))))) | |
"toggle" (fn [b i] (update b i (fn [i] (+ 2 i)))))] | |
(reduce f board coords))) | |
(defn main [] | |
(->> | |
(let [n 1000 | |
board (vec (repeat (* n n) 0))] | |
(reduce (fn [b line] (let [[cmd s e] (parse line)] | |
(update-board b cmd (get-coords n s e)))) | |
board | |
(str/split-lines (slurp "a6.txt")))) | |
(reduce +))) | |
(time (main)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment