Created
December 7, 2017 10:16
-
-
Save pavloo/a109753da5bc857b74877c31ed336a94 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; Part 1 | |
(defun sum (list) | |
"Solution for Part #1 http://adventofcode.com/2017/day/1" | |
(let ((s 0) next-i next val (n (length list))) | |
(dotimes (i n) | |
(progn | |
(setq val (nth i list)) | |
(setq next-i (+ i 1)) | |
(when (= next-i n) (setq next-i 0)) | |
(setq next (nth next-i list)) | |
(when (eq val next) (setq s (+ s val))) | |
) | |
) | |
s | |
) | |
) | |
(sum '(1 1 2 2)) ;; 3 | |
(sum '(1 1 1 1)) ;; 4 | |
(sum '(1 2 3 4)) ;; 0 | |
(sum '(9 1 2 1 2 1 2 9)) ;; 9 | |
(defun read-numbers-from-file (file-path) | |
(with-temp-buffer | |
(insert-file-contents file-path) | |
(mapcar 'string-to-number (split-string (string-trim (buffer-string)) "" t)) | |
) | |
) | |
(sum (read-numbers-from-file "./input.txt")) | |
;; Part 2 | |
(defun sum1 (list) | |
"Solution for Part #2 http://adventofcode.com/2017/day/1" | |
(let ((s 0) next-i next val d (n (length list))) | |
(setq d (/ n 2)) | |
(dotimes (i n) | |
(progn | |
(setq val (nth i list)) | |
(setq next-i (+ i d)) | |
(when (> next-i (1- n)) (setq next-i (- next-i n))) | |
(setq next (nth next-i list)) | |
(when (eq val next) (setq s (+ s val))) | |
) | |
) | |
(print s) | |
) | |
) | |
(sum1 '(1 2 1 2)) ;; 6 | |
(sum1 '(1 2 2 1)) ;; 0 | |
(sum1 '(1 2 3 4 2 5)) ;; 4 | |
(sum1 '(1 2 3 1 2 3)) ;; 12 | |
(sum1 '(1 2 1 3 1 4 1 5)) ;; 4 | |
(sum1 (read-numbers-from-file "./input1.txt")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment