From 81798f0ec7a09106bc81a11dd59b908866e5fe09 Mon Sep 17 00:00:00 2001 From: Alexandre Jesus Date: Sun, 31 Dec 2023 11:56:39 +0000 Subject: day11 --- day11.lisp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 day11.lisp (limited to 'day11.lisp') diff --git a/day11.lisp b/day11.lisp new file mode 100644 index 0000000..a5e6489 --- /dev/null +++ b/day11.lisp @@ -0,0 +1,46 @@ +(defun read-lines (filespec) + (with-open-file (stream filespec) + (loop for line = (read-line stream nil) + while line + collect line))) + +(defun empty-rows (lines) + (loop for line in lines + collect (every (lambda (c) (equal c #\.)) line))) + +(defun empty-cols (lines) + (loop for i below (length (first lines)) + collect (every (lambda (line) (equal (aref line i) #\.)) lines))) + +(defun galaxies-coords (lines erows ecols eincrement) + (loop for i = 0 then (+ i (if erow eincrement 1)) + for erow in erows + for line in lines + append (loop for j = 0 then (+ j (if ecol eincrement 1)) + for ecol in ecols + for c across line + if (equal c #\#) + collect (cons i j)))) + +(defun solve (filespec eincrement) + (let* ((lines (read-lines filespec)) + (erows (empty-rows lines)) + (ecols (empty-cols lines)) + (coords (galaxies-coords lines erows ecols eincrement))) + (loop for coord in coords + for rem on (cdr coords) + while rem + sum (loop for other in rem + sum (+ (abs (- (car coord) (car other))) + (abs (- (cdr coord) (cdr other)))))))) + +(defun solve1 (filespec) + (solve filespec 2)) + +(defun solve2 (filespec) + (solve filespec 1000000)) + +(print (solve1 "data/11/example.txt")) ; 374 +(print (solve1 "data/11/input.txt")) ; 9563821 +(print (solve2 "data/11/example.txt")) ; 82000210 +(print (solve2 "data/11/input.txt")) ; 827009909817 -- cgit v1.2.3