Created
January 3, 2018 16:57
-
-
Save pavloo/d928e03eeec8dfd41935fbce76e4f303 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 read-file-as-string (file-path) | |
(with-temp-buffer | |
(insert-file-contents file-path) | |
(mapcar 'string-trim (split-string (buffer-string) "," t)) | |
) | |
) | |
(defun calculate-dist (x y z) | |
(/ | |
(+ | |
(abs x) | |
(abs y) | |
(abs z) | |
) | |
2) | |
) | |
(defun hex-dist (lst) | |
(let ((x 0) (y 0) (z 0) (max 0) dist) | |
(dolist (step lst) | |
(cond | |
((string= step "ne") | |
(progn | |
(setq x (1+ x)) | |
(setq z (1- z)) | |
) | |
) | |
((string= step "se") | |
(progn | |
(setq x (1+ x)) | |
(setq y (1- y)) | |
) | |
) | |
((string= step "s") | |
(progn | |
(setq z (1+ z)) | |
(setq y (1- y)) | |
) | |
) | |
((string= step "sw") | |
(progn | |
(setq x (1- x)) | |
(setq z (1+ z)) | |
) | |
) | |
((string= step "nw") | |
(progn | |
(setq x (1- x)) | |
(setq y (1+ y)) | |
) | |
) | |
((string= step "n") | |
(progn | |
(setq y (1+ y)) | |
(setq z (1- z)) | |
) | |
) | |
) | |
(setq dist (calculate-dist x y z)) | |
(when (> dist max) (setq max dist)) | |
) | |
(print (calculate-dist x y z)) | |
(print max) | |
) | |
) | |
(hex-dist '("ne" "ne" "ne")) ;; 3 | |
(hex-dist '("ne" "ne" "sw" "sw")) ;; 0 | |
(hex-dist '("ne" "ne" "s" "s")) ;; 2 | |
(hex-dist '("se" "sw" "se" "sw" "sw")) ;; 3 | |
(hex-dist (read-file-as-string "./input.txt")) | |
(read-file-as-string "./input.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment