Skip to content

Instantly share code, notes, and snippets.

@pavloo
Created December 19, 2017 12:44
Show Gist options
  • Save pavloo/d64a4e005a28c7ea3b89c9b65bedf6ea to your computer and use it in GitHub Desktop.
Save pavloo/d64a4e005a28c7ea3b89c9b65bedf6ea to your computer and use it in GitHub Desktop.
(defun calculate-score (str)
(let ((list (split-string str "" t)) garbage (depth 0) (s 0) (g-count 0) ch)
(loop
for i from 0 to (1- (length list))
do
(progn
(setq ch (nth i list))
(cond
((string= ch "!")
(setq i (+ i 1))
)
((and (not (string= ch ">")) garbage)
(setq g-count (1+ g-count))
)
((string= ch "<")
(setq garbage t)
)
((string= ch ">")
(setq garbage nil)
)
((string= ch "{")
(progn
(setq depth (1+ depth))
)
)
((string= ch "}")
(setq s (+ s depth))
(setq depth (1- depth))
)
)
)
)
(list s g-count)
)
)
(calculate-score "{}") ;; 1
(calculate-score "{{{}}}") ;; 6
(calculate-score "{{},{}}") ;; 5
(calculate-score "{{{},{},{{}}}}") ;; 16
(calculate-score "{<a>,<a>,<a>,<a>}") ;; 1
(calculate-score "{{<ab>},{<ab>},{<ab>},{<ab>}}") ;; 9
(calculate-score "{{<!!>},{<!!>},{<!!>},{<!!>}}") ;; 9
(calculate-score "{{<a!>},{<a!>},{<a!>},{<ab>}}") ;; 3
(defun read-file-as-string (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(buffer-string)
)
)
(calculate-score (read-file-as-string "./input.txt"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment