Skip to content

Instantly share code, notes, and snippets.

@matthewbauer
Created December 4, 2024 20:37
Show Gist options
  • Save matthewbauer/10cb335c79f3374a761f120eabcf9a8d to your computer and use it in GitHub Desktop.
Save matthewbauer/10cb335c79f3374a761f120eabcf9a8d to your computer and use it in GitHub Desktop.
(defun substring2 (lists x1 y1 x2 y2)
(let ((i x1)
(j y1)
(res ""))
(while (and (or (/= i x2) (/= j y2))
(>= i 0)
(>= j 0)
(< j (length lists))
(< i (length (nth j lists))))
(setq res (concat res (substring (nth j lists) i (1+ i))))
(let ((progress nil))
(when (> x2 i)
(setq i (1+ i))
(setq progress t))
(when (> y2 j)
(setq j (1+ j))
(setq progress t))
(when (< x2 i)
(setq i (1- i))
(setq progress t))
(when (< y2 j)
(setq j (1- j))
(setq progress t))
(unless progress
(error "invalid arguments"))))
res))
(defun is-xmas (word)
(or (string= word "XMAS")
(string= word "SAMX")))
(defun is-mas (word)
(or (string= word "MAS")
(string= word "SAM")))
(let ((lines (with-temp-buffer
(insert-file-contents "day4.txt")
(split-string (buffer-string) "\n")))
(count 0))
(dotimes (i (length lines))
(let ((line (nth i lines)))
(setq j 0)
(dotimes (j (length line))
;; (when (is-xmas (substring2 lines j i (+ j 4) i)) (setq count (1+ count)))
;; (when (is-xmas (substring2 lines j i j (+ i 4))) (setq count (1+ count)))
;; (when (is-xmas (substring2 lines j i (+ j 4) (+ i 4))) (setq count (1+ count)))
;; (when (is-xmas (substring2 lines j i (- j 4) (+ i 4))) (setq count (1+ count)))
(when (and (is-mas (substring2 lines (- j 1) (- i 1) (+ j 2) (+ i 2)))
(is-mas (substring2 lines (+ j 1) (- i 1) (- j 2) (+ i 2))))
(setq count (1+ count))))))
count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment