Skip to content

Commit efa833f

Browse files
committed
Day 5
Added solution for day 5.
1 parent be7262e commit efa833f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

advent-of-code-2018.asd

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
(:file "day1")
1313
(:file "day2")
1414
(:file "day3")
15-
(:file "day4")))
15+
(:file "day4")
16+
(:file "day5")))

day5.lisp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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)))))))

0 commit comments

Comments
 (0)