(defpackage #:rock-paper-scissors
  (:use :cl))

(in-package #:rock-paper-scissors)

(defvar *games* (with-open-file (s "./input.txt")
		  (loop for line = (read-line s nil)
			while line collect (list
					     (1+ (- (char-code (char line 0)) (char-code #\A)))
					     (char line 2)))))

(defun move-str (x)
  (cond ((= x 1) "Rock")
	((= x 2) "Paper")
	((= x 3) "Scissors")
	(t (write-to-string x))))

(defun lose-against (x) (let ((tmp (mod (+ 2 x) 3))) (if (= tmp 0) 3 tmp)))
(defun win-against (x) (+ 1 (mod x 3)))
(defun draw-against (x) x)

(defun check-draw (opp me) (= (draw-against me) opp))
(defun check-win (opp me) (= (lose-against me) opp))
(defun check-loss (opp me) (= (win-against me) opp))

(defun score-game (game)
  (let ((opp (car game)) (outcome (car (cdr game))) (win-score 6) (draw-score 3) (loss-score 0))
    (let ((me (cond
	       ((eql #\X outcome) (lose-against opp))
	       ((eql #\Y outcome) (draw-against opp))
	       ((eql #\Z outcome) (win-against opp))
	       (t (format t "WTF (input)~%"))
	       )))
      (+ me (cond
	      ((check-draw opp me) draw-score)
	      ((check-win opp me) win-score)
	      ((check-loss opp me) loss-score)
	      (t (format t "WTF (outcome)~%")))))))

(let ((score (reduce #'+ (mapcar #'score-game *games*))))
  (format t "Total score: ~d~%" score))