summaryrefslogtreecommitdiffstats
path: root/day18.lisp
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2024-01-24 16:16:01 +0000
committerAlexandre Jesus <adbjesus@gmail.com>2024-01-24 16:16:01 +0000
commitf6351b48135cb886ba6bfc4e859b5382b9bcc607 (patch)
treee2c8a218ae14d6318b1ba0482c34f17a9c54986d /day18.lisp
parent4c79a9473347668ad10fdb243f0d51d71975a807 (diff)
downloadaoc2023-f6351b48135cb886ba6bfc4e859b5382b9bcc607.tar.gz
aoc2023-f6351b48135cb886ba6bfc4e859b5382b9bcc607.zip
day18
Diffstat (limited to 'day18.lisp')
-rw-r--r--day18.lisp63
1 files changed, 63 insertions, 0 deletions
diff --git a/day18.lisp b/day18.lisp
new file mode 100644
index 0000000..0b35af2
--- /dev/null
+++ b/day18.lisp
@@ -0,0 +1,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