blob: 0b35af2d8c37a22ef7115e69a52c7ce8767e59a5 (
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
(defun read-lines (filespec)
(with-open-file (stream filespec)
(loop for line = (read-line stream nil)
while line
collect line)))
(defun split-by-char (char string)
(loop for i = 0 then (1+ j)
as j = (position char string :start i)
collect (subseq string i j)
while j))
(defun compute-area (moves)
(let* ((coords '((0 . 0)))
(interior 0)
(bound 0 ))
(loop for m in moves
as dir = (car m)
as len = (cdr m)
as prv = (car coords)
as prvi = (car prv)
as prvj = (cdr prv)
as curi = (cond ((equal dir #\U) (- prvi len))
((equal dir #\D) (+ prvi len))
(t prvi))
as curj = (cond ((equal dir #\L) (- prvj len))
((equal dir #\R) (+ prvj len))
(t prvj))
as cur = (cons curi curj)
do (setf coords (cons cur coords)
interior (+ interior (- (* prvi curj) (* prvj curi)))
bound (+ bound (abs (+ (- curi prvi) (- curj prvj))))))
(1+ (ash (+ (abs interior) bound) -1))))
(defun get-move (line)
(let ((aux (split-by-char #\Space line)))
(cons (aref (car aux) 0) (parse-integer (cadr aux)))))
(defun get-moves (lines)
(mapcar #'get-move lines))
(defun solve1 (filespec)
(let* ((lines (read-lines filespec))
(moves (get-moves lines)))
(compute-area moves)))
(defun get-swapped-move (line)
(let* ((aux (caddr (split-by-char #\Space line)))
(aux (parse-integer aux :start 2 :radix 16 :junk-allowed t)))
(cons (aref "RDLU" (mod aux 16)) (ash aux -4))))
(defun get-swapped-moves (lines)
(mapcar #'get-swapped-move lines))
(defun solve2 (filespec)
(let* ((lines (read-lines filespec))
(moves (get-swapped-moves lines)))
(compute-area moves)))
(print (solve1 "data/18/example.txt")) ;; 62
(print (solve1 "data/18/input.txt")) ;; 34329
(print (solve2 "data/18/example.txt")) ;; 952408144115
(print (solve2 "data/18/input.txt")) ;; 42617947302920
|