Last active
June 25, 2026 19:03
-
-
Save joinr/af6fb9e5a97069e8601030654d7f4eb2 to your computer and use it in GitHub Desktop.
aocarrayjank
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
| (ns aoc | |
| (:require [clojure.string :as str])) | |
| (set! *warn-on-reflection* true) | |
| (set! *unchecked-math* :warn-on-boxed) | |
| (def op->num {:turn-on 0 | |
| :turn-off 1 | |
| :toggle 2}) | |
| (defn parse-line [line] | |
| (let [[_ op x1 y1 x2 y2] | |
| (re-matches #"(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)" line)] | |
| {:op (op->num (keyword (str/replace op #" " "-"))) | |
| :x1 (Long/parseLong x1) :y1 (Long/parseLong y1) | |
| :x2 (Long/parseLong x2) :y2 (Long/parseLong y2)})) | |
| (defn do-op | |
| {:inline (fn [op v] `(case ~op | |
| 0 (inc ~v) | |
| 1 (max 0 (dec ~v)) | |
| 2 (+ ~v 2))) | |
| :inline-arities #{2}} | |
| ^long [^long op ^long v] | |
| (case op | |
| 0 (inc v) | |
| 1 (max 0 (dec v)) | |
| 2 (+ v 2))) | |
| (defn apply-instr! [^longs grid {:keys [op x1 y1 x2 y2]}] | |
| (let [x1 (long x1) | |
| x2 (long x2) | |
| y1 (long y1) | |
| y2 (long y2) | |
| op (long op)] | |
| (loop [x x1] | |
| (when (<= x x2) | |
| (loop [y y1] | |
| (when (<= y y2) | |
| (let [i (+ (* x 1000) y)] | |
| (aset grid i (do-op op (aget grid i)))) | |
| (recur (inc y)))) | |
| (recur (inc x)))) | |
| grid)) | |
| (defn solve [input] | |
| (let [grid (long-array 1000000)] | |
| (->> (str/split-lines input) | |
| (map parse-line) | |
| (reduce apply-instr! grid)) | |
| (areduce grid i sum 0 (+ sum (aget grid i))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment