aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol.scm (1763B) [raw]


      1 #!/usr/local/bin/chicken-csi -ss
      2 (import (chicken io)
      3         (chicken string))
      4 
      5 ; A, X - Rock
      6 ; B, Y - Paper
      7 ; C, Z - Scissors
      8 (define (decode-1 str)
      9   (cond ((or (equal? str "A") (equal? str "X")) 'R)
     10         ((or (equal? str "B") (equal? str "Y")) 'P)
     11         ((or (equal? str "C") (equal? str "Z")) 'S)
     12         (else (error "Bad decode"))))
     13 
     14 ; A, B, C - Rock, Paper, Scissors
     15 ; X, Y, Z - Lose, Draw, Win
     16 (define (decode-2 round)
     17   (let ((a (car round))
     18         (b (cadr round)))
     19     (cond ((and (equal? a "A") (equal? b "X")) '(R S))
     20           ((and (equal? a "A") (equal? b "Y")) '(R R))
     21           ((and (equal? a "A") (equal? b "Z")) '(R P))
     22           ((and (equal? a "B") (equal? b "X")) '(P R))
     23           ((and (equal? a "B") (equal? b "Y")) '(P P))
     24           ((and (equal? a "B") (equal? b "Z")) '(P S))
     25           ((and (equal? a "C") (equal? b "X")) '(S P))
     26           ((and (equal? a "C") (equal? b "Y")) '(S S))
     27           ((and (equal? a "C") (equal? b "Z")) '(S R))
     28           (else (error "Bad decode")))))
     29 
     30 (define (score-sym s)
     31   (cond ((eq? s 'R) 1)
     32         ((eq? s 'P) 2)
     33         ((eq? s 'S) 3)
     34         (else (error "Bad sym"))))
     35 
     36 (define (beats? a b)
     37   (or (and (eq? a 'R) (eq? b 'S))
     38       (and (eq? a 'S) (eq? b 'P))
     39       (and (eq? a 'P) (eq? b 'R))))
     40 
     41 (define (score-round round)
     42   (let* ((opp (car round))
     43          (you (cadr round)))
     44     (+ (score-sym you)
     45        (cond ((beats? opp you) 0)
     46              ((beats? you opp) 6)
     47              (else 3)))))
     48 
     49 (define (main args)
     50   (let* ((lines (read-lines))
     51          (rounds-1 (map (lambda (l) (map decode-1 (string-split l))) lines))
     52          (rounds-2 (map decode-2 (map string-split lines))))
     53     (print (apply + (map score-round rounds-1)))
     54     (print (apply + (map score-round rounds-2)))))