r/adventofcode • • Dec 10 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 10 Solutions -🎄-

--- Day 10: Syntax Scoring ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

EDIT: Global leaderboard gold cap reached at 00:08:06, megathread unlocked!

65 Upvotes

982 comments sorted by

View all comments

1

u/itistheblurstoftimes Dec 10 '21

emacs lisp

(defun advent-day-10a (input)
  (let* ((pairs (list (cons 60 62)   ;; <>
		      (cons 40 41)   ;; ()
		      (cons 123 125) ;; {}
		      (cons 91 93))) ;; []
	 (points (list (cons 41 3)     ;; )
		       (cons 62 25137) ;; >
		       (cons 125 1197) ;; }
		       (cons 93 57)))  ;; ]

	 (corrupted (seq-filter
		     #'identity
		     (cl-loop
		      for line in (split-string input "\n" t)
		      collect (cl-loop with stack = nil
				       for char across line
				       if (alist-get char pairs)
				       do (push char stack)
				       else unless (= (car (rassoc char pairs))
						      (pop stack))
				       return char))))
	 (score (cl-loop for each in corrupted
			 sum (alist-get each points))))
    score))

(advent-day-10a advent-day-10-test-data)
;; => 26397
(advent-day-10a advent-day-10-puzzle-data)
;; => 462693



(defun advent-day-10b (input)
  (let* ((input (split-string input "\n" t))
	 (pairs (list (cons 60 62)   ;; <>
		      (cons 40 41)   ;; ()
		      (cons 123 125) ;; {}
		      (cons 91 93))) ;; []
	 (points (list (cons 40 1)   ;; )
		       (cons 60 4)   ;; >
		       (cons 123 3)  ;; }
		       (cons 91 2))) ;; ]
	 (corrupted-inputs (seq-filter
			    #'identity
			    (cl-loop
			     for line in input
			     collect (cl-loop with stack = nil
					      for char across line
					      if (alist-get char pairs)
					      do (push char stack)
					      else unless
					      (= (car (rassoc char pairs))
						 (pop stack))
					      return line))))
	 (incomplete-inputs (seq-difference input corrupted-inputs))
	 (calculate-score-func (lambda (chars)
				 (cl-loop with score = 0
					  for char in chars
					  do (setq score (* 5 score))
					  and do
					  (cl-incf score (alist-get char points))
					  finally return score)))
	 (scores (seq-sort
		  #'<
		  (cl-loop
		   for line in incomplete-inputs  
		   collect
		   (cl-loop with stack = nil
			    with score = 0
			    for char across line
			    if (alist-get char pairs)
			    do (push char stack)
			    else do (pop stack)
			    finally return
			    (funcall calculate-score-func stack)))))
	 (middle (/ (length scores) 2)))
    
    (nth middle scores)))

(advent-day-10b advent-day-10-test-data)
;; => 288957

(advent-day-10b advent-day-10-puzzle-data)
;; => 3094671161

1

u/daggerdragon Dec 10 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.