Skip to content

Instantly share code, notes, and snippets.

@Arnot
Created April 17, 2018 15:12
Show Gist options
  • Save Arnot/10502e9c5562a5fa9206569c6b4cbcec to your computer and use it in GitHub Desktop.
Save Arnot/10502e9c5562a5fa9206569c6b4cbcec to your computer and use it in GitHub Desktop.
(defstruct state (sum 0) (garbage-sum 0) (group-depth 0) in-garbage after-!)
(defun process-character (s character)
(if (state-in-garbage s)
(if (state-after-! s)
(setf (state-after-! s) nil)
;; Process stuff inside garbage
(case character
(?! (setf (state-after-! s) t))
(?> (setf (state-in-garbage s) nil))
(t (incf (state-garbage-sum s)))
))
;; Process stuff outside of garbage
(case character
(?! (setf (state-after-! s) t))
(?< (setf (state-in-garbage s) t))
(?> (setf (state-in-garbage s) nil))
(?{ (unless (state-in-garbage s)
(incf (state-group-depth s))))
(?} (unless (state-in-garbage s)
(incf (state-sum s)
(state-group-depth s))
(decf (state-group-depth s))))))
s)
(defun process-input ()
(with-current-buffer "day9input"
(goto-char (point-min))
(let ((state (make-state)))
(while (not (eobp))
(process-character state (char-after))
(forward-char))
state)))
(let ((result (process-input)))
(print (format "Part 1: %s" (state-sum result)))
(print (format "Part 2: %s" (state-garbage-sum result))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment