summaryrefslogtreecommitdiffstats
path: root/day11.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'day11.lisp')
-rw-r--r--day11.lisp46
1 files changed, 46 insertions, 0 deletions
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