aoc-cl/src/2024/day02.lisp
2024-12-02 08:53:49 +01:00

61 lines
1.3 KiB
Common Lisp

(defpackage :aoc/2024/02
(:use :cl :aoc :alexandria)
(:export
#:sample-data
#:part1
#:part2
#:deltas
#:safe-levels
#:safe-levels-with-dampener
))
(in-package :aoc/2024/02)
(defun parse-line (s)
(map 'list #'parse-integer (cl-ppcre:split " " s)))
(defvar input-data '())
(setf input-data (map 'list #'parse-line (test-input 2024 2)))
(defvar sample-text "")
(setf sample-text (aoc:split-lines "7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
"))
(defvar sample-data '())
(setf sample-data (map 'list #'parse-line sample-text))
(defun deltas (levels)
(map 'list #'- (subseq levels 1) levels))
(defun safe-levels (levels)
(let ((ds (deltas levels)))
(and
(or (every #'plusp ds) (every #'minusp ds))
(every (lambda (d) (>= 3 (abs d))) ds))))
(defun remove-nth (n xs)
(append (subseq xs 0 (1- n)) (nthcdr n xs)))
(defun safe-levels-with-dampener (levels)
(or (safe-levels levels)
(loop for n from 1 to (length levels)
thereis (safe-levels (remove-nth n levels)))))
(defun part1 (data)
(loop for report in data
count (safe-levels report)))
(defun part2 (data)
(loop for report in data
count (safe-levels-with-dampener report)))
(defun solve-day ()
(format t "part1: ~A~%" (part1 input-data))
(format t "part2: ~A~%" (part2 input-data)))