Skip to content

Instantly share code, notes, and snippets.

@pavloo
Created December 7, 2017 21:46
Show Gist options
  • Save pavloo/0292fc1f172884561d8a1a706787065d to your computer and use it in GitHub Desktop.
Save pavloo/0292fc1f172884561d8a1a706787065d to your computer and use it in GitHub Desktop.
(defun read-numbers-from-file (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(mapcar
(lambda (arg) (mapcar 'string-to-number(split-string arg)))
(split-string (string-trim (buffer-string)) "\n" t)
)
)
)
(read-numbers-from-file "./input.txt")
(defun find-max-min (compar list)
(let ((res (nth 0 list)))
(dolist (x list)
(when (apply compar (list x res)) (setq res x))
)
res
)
)
(find-max-min '< '(-1 2 3))
(defun calculate-checksum (list2d)
"Solution for Part #1 http://adventofcode.com/2017/day/2"
(let (res (min 0) (max 0))
(dolist (list list2d)
(setq min (find-max-min '< list))
(setq max (find-max-min '> list))
(push (- max min) res)
)
(apply '+ res)
)
)
(setq input '((5 1 9 5)
(7 5 3)
(2 4 6 8)))
(calculate-checksum input) ;; 18
(calculate-checksum (read-numbers-from-file "./input.txt"))
(defun find-evenly-devided (list)
(let ((n (length list)) val next j res)
(dotimes (i (- n 1))
(dotimes (jj (- n i 1))
(setq j (+ jj i 1))
(setq val (nth i list))
(setq next (nth j list))
(when (= (% val next) 0) (setq res (/ val next)))
(when (= (% next val) 0) (setq res (/ next val)))
)
)
res
)
)
(defun calculate-checksum1 (list2d)
"Solution for Part #2 http://adventofcode.com/2017/day/2"
(let (res step)
(dolist (list list2d)
(setq step (find-evenly-devided list))
(when step (push step res))
)
(apply '+ res)
)
)
(setq input1 '((5 9 2 8)
(9 4 7 3)
(3 8 6 5)))
(calculate-checksum1 (read-numbers-from-file "./input.txt"))
(find-evenly-devided (nth 2 input1)) ;; 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment