blob: a5e6489cf4d73e248c2535edaaf4000cbe1dd952 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
|