Created
December 19, 2017 12:44
-
-
Save pavloo/d64a4e005a28c7ea3b89c9b65bedf6ea to your computer and use it in GitHub Desktop.
This file contains 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
(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