File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 12
12
(:file " day1" )
13
13
(:file " day2" )
14
14
(:file " day3" )
15
- (:file " day4" )))
15
+ (:file " day4" )
16
+ (:file " day5" )))
Original file line number Diff line number Diff line change
1
+ ; ;;; day5.lisp
2
+
3
+ (in-package :advent-of-code-2018 )
4
+
5
+ (defun day5-next-unit (string &optional (start 0 ))
6
+ (position-if (lambda (c) (not (equal c #\0 ))) string :start start))
7
+
8
+ (defun day5-prior-unit (string end)
9
+ (or (position-if (lambda (c) (not (equal c #\0 ))) string :from-end t :end end)
10
+ 0 ))
11
+
12
+ (defun day5-react! (polymer)
13
+ (loop
14
+ :for index1 := 0 :then index2
15
+ :for index2 := (day5-next-unit polymer (1+ index1))
16
+ :while index2
17
+ :when (and (equalp (char polymer index1) (char polymer index2))
18
+ (not (equal (char polymer index1) (char polymer index2))))
19
+ :do (setf (char polymer index1) #\0 (char polymer index2) #\0 index2 (day5-prior-unit polymer index1))
20
+ :finally (return (remove #\0 polymer))))
21
+
22
+ (defun day5 ()
23
+ (let ((base-polymer (day5-react! (first (read-puzzlefile " input5.txt" )))))
24
+ (format t " The reacted polymer has still length ~a .~% " (length base-polymer))
25
+ (format t " With removing the right unit, the shortest possible polymer has length ~a .~% "
26
+ (loop
27
+ :for problematic-unit :from (char-code #\a ) :upto (char-code #\z )
28
+ :minimize (length (day5-react! (remove (code-char problematic-unit)
29
+ base-polymer
30
+ :test #' equalp )))))))
You can’t perform that action at this time.
0 commit comments