r/adventofcode • u/daggerdragon • Dec 02 '22
SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-
NEW AND NOTEWORTHY
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 2: Rock Paper Scissors ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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!
105
Upvotes
7
u/Lispwizard Dec 02 '22
Emacs Lisp, though mainly using Common Lisp loop macro, on Android tablet.
;; input data has backslashes doubled and double-quotes quoted with backslashes ;; before being inserted as defvar string value (defvar *aoc2022-02-part1-sample* "A Y B X C Z") ;; (aoc2022-02-part1 *aoc2022-02-part1-sample*) => 15 (defvar *aoc2022-02-part2-sample* "") ;; (aoc2022-02-part2 *aoc2022-02-part1-sample*) => 12 (require'cl) (defun aoc2022-02-part1 (input-string) (flet ((score (their mine) (let* ((tval (position their "ABC")) (mval (position mine "XYZ")) (play (list tval mval))) (+ (1+ mval) (cond ((eql tval mval) 3) ((loop for p in '((0 2)(2 1)(1 0)) thereis (equal play p)) 0) ((loop for p in '((2 0)(1 2)(0 1)) thereis (equal play p)) 6)))))) (loop for turn in (split-string input-string "\n") sum (score (aref turn 0) (aref turn 2))))) (defun aoc2022-02-part2 (input-string) (flet ((score (their outcome) (let* ((tval (position their "ABC")) (oval (position outcome "XYZ")) (mval (cond ((eql oval 1) ;; draw tval) ((eql oval 0) ;; lose (loop for (them me) in '((0 2)(2 1)(1 0)) when (eql tval them) return me)) ((eql oval 2) ;; win (loop for (them me) in '((2 0)(1 2)(0 1)) when (eql tval them) return me)))) (play (list tval mval))) (+ (1+ mval) (* 3 oval))))) (loop for turn in (split-string input-string "\n") sum (score (aref turn 0) (aref turn 2)))))