Day 4 - common lisp

This commit is contained in:
Simponic 2022-12-03 22:29:10 -07:00
parent 5204d36f6b
commit cb728f5961
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
2 changed files with 35 additions and 1 deletions

View File

@ -8,7 +8,8 @@
- [Day 1](./day-1) - [Day 1](./day-1)
- [x] Clojure - [x] Clojure
- [Day 2](./day-2) - [Day 2](./day-2)
- [] Common LISP - [x] Common LISP
- [Day 3](./day-3)
- [] C++ - [] C++
- [] Dart - [] Dart
- [] Elixir - [] Elixir

33
day-03/sol.lisp Normal file
View File

@ -0,0 +1,33 @@
(ql:quickload "cl-ppcre")
(defun get-ranges (line)
(ppcre:register-groups-bind ((#'parse-integer first second third fourth))
("(\\d+)-(\\d+),(\\d+)-(\\d+)" line :sharedp t)
`((,first ,second) (,third ,fourth))))
(defun ranges-may-subset-of-one (a b)
(member-if
(lambda (r)
(and
(>= (caar r) (caadr r))
(<= (cadar r) (cadadr r))))
`((,a ,b) (,b ,a))))
(defun range-is-contained-at-all (a b)
(and
(<= (car a) (cadr b))
(>= (cadr a) (car b))))
(defun main ()
(let ((lines (uiop:read-file-lines "input")))
(print
(mapcar (lambda (f)
(reduce (lambda (a x)
(if (apply f (get-ranges x))
(1+ a)
a))
lines
:initial-value 0))
'(range-is-contained range-may-be-subset-of-one)))))
(main)