summaryrefslogtreecommitdiff
path: root/day-03
diff options
context:
space:
mode:
authorSimponic <loganhunt@simponic.xyz>2022-12-03 22:29:10 -0700
committerSimponic <loganhunt@simponic.xyz>2022-12-03 22:29:10 -0700
commitcb728f596152a5ac13b434859112570a9b28ca13 (patch)
treeed3d764b2666f7fc4de3cc1b062ba674ade17d2c /day-03
parent5204d36f6b856abe15a013cfab190baaec1eba19 (diff)
downloadaoc-cb728f596152a5ac13b434859112570a9b28ca13.tar.gz
aoc-cb728f596152a5ac13b434859112570a9b28ca13.zip
Day 4 - common lisp
Diffstat (limited to 'day-03')
-rw-r--r--day-03/sol.lisp33
1 files changed, 33 insertions, 0 deletions
diff --git a/day-03/sol.lisp b/day-03/sol.lisp
new file mode 100644
index 0000000..7f9ba90
--- /dev/null
+++ b/day-03/sol.lisp
@@ -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)