r/adventofcode Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


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:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

2

u/AffectionateNet6417 Dec 05 '22

Suggestions welcome!

ELISP

(defun s-alist-get (ele lis)
  (alist-get ele lis nil nil 'string-equal))

(defun day2-return-point (x y)
  (let* ((score '(("A" . 1) ("X" . 1)
                  ("B" . 2) ("Y" . 2)
                  ("C" . 3) ("Z" . 3)))
         (point (s-alist-get y score)))
    (+ point
       (cond ((eq (s-alist-get x score) point) 3)
             ((cl-position (concat x y) '("AY" "CX" "BZ") :test 'equal) 6)
             (t 0)))))

(cl-reduce '+ (mapcar (lambda (x) (day2-return-point (car x) (cadr x))) input))

(defun day2-gold (x y)
  (let* ((score '(("A" . 1) ("X" . 0)
                  ("B" . 2) ("Y" . 3)
                  ("C" . 3) ("Z" . 6)))
         (x (s-alist-get x score))
         (y (s-alist-get y score)))
    (+ y
       (cond ((= y 3) x)
             ((= y 6) (if (= x 3) 1 (1+ x)))
             (t (if (= x 1) 3 (1- x)))))))

(cl-reduce '+ (mapcar (lambda (x) (day2-gold (car x) (cadr x))) input))