(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)))
(1+ (- (char-code (char line 2)) (char-code #\X)))))))
(defun move-str (x)
(cond ((= x 1) "Rock")
((= x 2) "Paper")
((= x 3) "Scissors")
(t (write-to-string x))))
(defun check-win (opp me) (= (mod (+ 1 opp) 3) (mod me 3)))
(defun check-loss (opp me) (= (mod (+ 2 opp) 3) (mod me 3)))
(defun score-game (game)
(let ((opp (car game))(me (car (cdr game))) (win-score 6) (draw-score 3) (loss-score 0))
(+ me (cond ((= opp me) draw-score)
((check-win opp me) win-score)
((check-loss opp me) loss-score)
(t (format t "WTF~%"))))))
(let ((score (reduce #'+ (mapcar #'score-game *games*))))
(format t "Total score: ~d~%" score))