r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:10:20!

32 Upvotes

515 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 13 '18

Common lisp:

Same thing, but shifting everything over to lisp instead.

(defun react (seq)
  (loop with stack
        for c in seq
        do (if (and stack (= (logxor (first stack) c) #x20))
               (pop stack)
               (push c stack))
        finally (return (reverse stack))))

(defun main ()
  (let* ((input (with-open-file (in "05.input")
                  (loop for c across (read-line in)
                        collect (char-code c))))
         (reduced (react input))
         (min-len (loop for c1 from (char-code #\a) to (char-code #\z)
                        for filtered = (remove-if-not (lambda (c2) (/= (logxor c1 c2) #x20 #x0)) reduced)
                        minimize (length (react filtered)))))
    (format t "Result 5a: ~d~%" (length reduced))
    (format t "Result 5b: ~d~%" min-len)))

;; CL-USER> (time (main))
;; Result 5a: 10598
;; Result 5b: 5312
;; Evaluation took:
;;   0.025 seconds of real time
;;   0.024622 seconds of total run time (0.024622 user, 0.000000 system)
;;   100.00% CPU
;;   53,304,082 processor cycles
;;   14,478,240 bytes consed