Last active
May 23, 2018 14:54
-
-
Save Arnot/da4f332f6c6290d4e63a04f4db9acb52 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
;; -*- lexical-binding: t -*- | |
(defstruct scanner | |
(depth 0) | |
(range 0)) | |
(defun score (scanner &optional delay) | |
(unless delay (setf delay 0)) | |
(if (= 0 (mod (+ (scanner-depth scanner) delay) | |
(* 2 (1- (scanner-range scanner))))) | |
(* (scanner-depth scanner) | |
(scanner-range scanner)) | |
0)) | |
(defun hits (scanner &optional delay) | |
(unless delay (setf delay 0)) | |
(if (= 0 (mod (+ (scanner-depth scanner) delay) | |
(* 2 (1- (scanner-range scanner))))) | |
1 | |
0)) | |
(defun build-scanner (s) | |
(let* ((tokens (split-string s ": " t)) | |
(numbers (mapcar #'string-to-number tokens))) | |
(make-scanner :depth (first numbers) | |
:range (second numbers)))) | |
(defun get-scanners (input) | |
(let ((lines (split-string input "\n" t))) | |
(mapcar #'build-scanner lines))) | |
(defun get-score (input &optional delay) | |
(apply #'+ (mapcar (lambda (s) (score s delay)) (get-scanners input)))) | |
(defun get-hits (input &optional delay) | |
(apply #'+ (mapcar (lambda (s) (hits s delay)) (get-scanners input)))) | |
(let (result) | |
(dotimes (i 15) | |
(push (get-hits input i) result)) | |
(reverse result)) | |
(setf input | |
(concat | |
"0: 3\n" | |
"1: 2\n" | |
"4: 4\n" | |
"6: 4\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment