Advent of Code Solutions Thread: Day Two

Only shitty solutions allowed

38
Jump in the discussion.

No email address required.

Emacs Lisp

Accidentally overdid it now that I see the other answers where everyone just fucking hardcoded it

But I guess i'll keep doing them all the hard way

(set-buffer "input.txt")
(goto-char (point-min))

;; The score for a single round is the score for the shape you selected (1 for
;; Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the
;; round (0 if you lost, 3 if the round was a draw, and 6 if you won).

(defconst rock 1) (defconst paper 2) (defconst scissors 3)
(defconst lost 0) (defconst draw 3) (defconst won 6)

(defun opponent-player-at-line ()
  "The opponent's choice and the players choice"
  (let ((plyr nil)
        (oppo nil))
    (setq oppo (string-trim (thing-at-point 'word t)))
    (setq oppo
          (pcase oppo ("A" 'rock) ("B" 'paper) ("C" 'scissors)))
    (forward-char) (forward-char)

    (setq plyr (string-trim (thing-at-point 'word t)))
    (setq plyr
          (pcase plyr ("X" 'rock) ("Y" 'paper) ("Z" 'scissors)))
    (list oppo plyr)))

(defun opponent-outcome-at-line ()
  "The opponent's choice and the outcome needed"
  (let ((outcome nil)
        (oppo nil))
    (setq oppo (string-trim (thing-at-point 'word t)))
    (setq oppo
          (pcase oppo ("A" 'rock) ("B" 'paper) ("C" 'scissors)))
    (forward-char) (forward-char)

    (setq outcome (string-trim (thing-at-point 'word t)))
    (setq outcome
          (pcase outcome ("X" 'lost) ("Y" 'draw) ("Z" 'won)))
    (list oppo outcome)))

(defun player-game-result-points (oppo plyr)
  "For part 1"
  (if (eq oppo plyr)
      'draw
    (if (or (and (eq oppo 'rock)     (eq plyr 'scissors))
            (and (eq oppo 'scissors) (eq plyr 'paper))
            (and (eq oppo 'paper)    (eq plyr 'rock)))
        'lost
      'won)))

(defun player-choice-from-outcome (oppo outcome)
  "For part 2"
  (if (eq outcome 'draw)
      ;; Just do the opponents one then
      oppo
    (pcase outcome
      ('lost (pcase oppo ('rock 'scissors) ('paper 'rock) ('scissors 'paper)))
      ('won (pcase oppo ('rock 'paper) ('paper 'scissors) ('scissors 'rock))))))

;; Part 1
(let ((total 0)
      (oppo-plyr-sym nil))
  (while (and (not (eobp)))
    (setq oppo-plyr-sym (opponent-player-at-line))
    
    (setq total (+ total
                   (eval (apply 'player-game-result-points oppo-plyr-sym))
                   (eval (nth 1 oppo-plyr-sym))))
    
    (forward-line)
    (beginning-of-line))
  (message "Part one answer: %s" total))

(goto-char (point-min))

;; Part 2
(let ((total 0)
      (oppo-outcome-sym nil))
  (while (and (not (eobp)))
    (setq oppo-outcome-sym (opponent-outcome-at-line))
    ;; (message "opp does %s so player chose %s for outcome of %s" (car
    ;;                       oppo-outcome-sym) (apply 'player-choice-from-outcome
    ;;                       oppo-outcome-sym)
    ;;                       (nth 1 oppo-outcome-sym))
    (setq total (+ total
                   (eval (apply
                          'player-choice-from-outcome
                          oppo-outcome-sym))
                   ;; Same as last time lol
                   (eval (nth 1 oppo-outcome-sym))))
    
    (forward-line)
    (beginning-of-line))
  (message "Part two answer: %s" total))
Jump in the discussion.

No email address required.

That's nice sweaty. Why don't you have a seat in the time out corner with Pizzashill until you calm down, then you can have your Capri Sun.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.