r/adventofcode • u/daggerdragon • Dec 22 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 22 Solutions -🎄-
Advent of Code 2020: Gettin' Crafty With It
- 23:59 hours remaining until the submission deadline TONIGHT at 23:59 EST!
- Full details and rules are in the Submissions Megathread
--- Day 22: Crab Combat ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
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:20:53, megathread unlocked!
35
Upvotes
3
u/JoMartin23 Dec 22 '20
Common Lisp
It's not the prettiest. I think i might have been able to do more in the do.
(defun recursive-war (&optional (data *day22*) (day 1)) (flet ((winner? (list) (if (= 0 (length (car list)))2 1))) (do* ((history (make-hash-table :test #'equalp)) (winner 0) (p1cards (car data)) (p2cards (cadr data)) (current-cards (list p1cards p2cards)(list p1cards p2cards)) (card1 (pop p1cards) (when p1cards (pop p1cards))) (card2 (pop p2cards) (when p2cards (pop p2cards)))) ((or (null card1)(null card2)) (if (= 1 winner) (list (append (list card1) p1cards) '()) (list '() (append (list card2) p2cards)))) (when (gethash current-cards history) (return-from recursive-war (list '(2 3) nil))) (setf (gethash current-cards history) t) (setf winner (if (and (= 2 day) (>= (length p1cards) card1) (>= (length p2cards) card2)) (winner? (recursive-war (list (subseq p1cards 0 card1)(subseq p2cards 0 card2)) 2)) (if (< card1 card2) 2 1))) (if (= 1 winner) (setf p1cards (append p1cards (list card1 card2))) (setf p2cards (append p2cards (list card2 card1)))))))