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

1

u/AdventOfELisp Dec 02 '22

Elisp with cl module

(defun letter-to-number (xyz)
  (cond ((string= xyz "X") 1)
	((string= xyz "Y") 0)
	((string= xyz "Z") 2)
	((string= xyz "A") 0)
	((string= xyz "B") 1)
	((string= xyz "C") 2)
	('t xyz)))

(defun parse-input (str)
  (mapcar (lambda (x) (mapcar #'letter-to-number (split-string x " ")))
	  (split-string str "\n")))

(defun calculate-score (round)  
  (let* ((enemy (car round))
	 (difference (cadr round))
	 (you (% (+ (- enemy difference) 3) 3)))
    (+ (1+ you)
     (cond ((= difference 0) 3)
	   ((= difference 1) 0)
	   ((= difference 2) 6)))))

(let
    ((input (parse-input (f-read "input"))))
  (apply '+ (mapcar #'calculate-score input)))

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.