diff --git a/common_lisp/2015/1/problem.lisp b/common_lisp/2015/1/problem.lisp new file mode 100644 index 0000000..8e3b95a --- /dev/null +++ b/common_lisp/2015/1/problem.lisp @@ -0,0 +1,23 @@ +(require :uiop) + +(defun input-string () (uiop:read-file-string "../../../data/2015/1/input.txt")) + +(defun floor-from-next-char (floor char) + (if (char= #\( char) (1+ floor) (if (char= #\) char) (1- floor) floor))) + +(defun floor-from-string (string) + (reduce #'floor-from-next-char string :initial-value 0)) + +(defun steps-from-string (string) +(cdr (reduce (lambda (a b) + (let ((floor (car a)) + (steps (cdr a))) + (if (> 0 floor) (cons floor steps) + (cons (floor-from-next-char floor b) (1+ steps))))) + string :initial-value (cons 0 0)))) + +(defun part-1 () (format t "Part 1: ~a" (floor-from-string (input-string)))) +(defun part-2 () (format t "Part 2: ~a" (steps-from-string (input-string)))) +(defun solution () (part-1) (terpri) (part-2) (terpri)) + +(solution)