Elisp programming is fun :)

i just wrote a solution for adventofcode 2018 day 10 (Day 10 - Advent of Code 2018) in emacs lisp. it was really fun writing the iterations to another buffer :slight_smile: in case anybody’s interested, here’s my (not particularly emacs-like) code. if anybody has any suggestions for improvement, please let me know!

;; -*- lexical-binding: t; -*-

(setq lexical-binding t)

(require 'dash)
(require 's)
(require 'f)

(defun day10--parse-line (line)
  (cl-labels ((parse-pair (str)
		(->> str
		     (s-split ",")
		     (-map #'string-to-number))))
    (->> line
	 (s-split ">")
	 (-take 2)
	 (-map (lambda (p) (s-split "<" p)))
	 (-map #'cl-second)
	 (-map #'parse-pair))))

(defun day10--advance-coord (coord)
  (cl-destructuring-bind ((x y) (vx vy)) coord
    (list (list (+ x vx) (+ y vy)) (list vx vy))))

(defun day10--loop (coords)
  (-map #'day10--advance-coord coords))

(defun day10--print (elems)
  (let* ((coords (-map #'car elems))
	 (max-x (->> coords
		     (-map #'car)
	 (min-x (->> coords
		     (-map #'car)
	 (max-y (->> coords
		     (-map #'cl-second)
	 (min-y (->> coords
		     (-map #'cl-second)
	 (height (1+ (- max-y min-y)))
	 (width (1+ (- max-x min-x)))
	 (buf (get-buffer-create "*display*")))
    (if (or (> height 100) (> width 100))
      (cl-labels ((get-coord (p)
		    (list (- (car p) min-x)
			  (- (cl-second p) min-y))))
	(with-current-buffer buf
	  (dotimes (j height)
	    (dotimes (i width)
	      (insert ","))
	    (insert "\n"))
	  (dolist (coord coords)
	    (cl-destructuring-bind (x y) (get-coord coord)
	      (goto-line (1+ y))
	      (move-to-column x)
	      (delete-char 1)
	      (insert "X")

(defun day10-part1and2 (filename)
  (let ((coords (->> filename
		     (-map #'day10--parse-line))))
    (named-let my-loop ((coords coords) (i 0))
      (if (not (day10--print coords))
	  (message "too big")
	  (message "loop: %d" i)
      (my-loop (day10--loop coords) (1+ i)))))

(day10-part1and2 "input.txt")