day 2: cheese
This commit is contained in:
parent
dc612a29a4
commit
591590eb05
52
2.rkt
52
2.rkt
|
@ -5,49 +5,23 @@
|
|||
;; solution for day 2
|
||||
|
||||
(define (part1 input)
|
||||
(for/sum ([x (in-list input)])
|
||||
(match-define (list x1 x2) x)
|
||||
(+ (match x2
|
||||
["X" 1]
|
||||
["Y" 2]
|
||||
["Z" 3])
|
||||
(match* (x1 x2)
|
||||
[("A" "X") 3]
|
||||
[("A" "Y") 6]
|
||||
[("A" "Z") 0]
|
||||
[("B" "X") 0]
|
||||
[("B" "Y") 3]
|
||||
[("B" "Z") 6]
|
||||
[("C" "X") 6]
|
||||
[("C" "Y") 0]
|
||||
[("C" "Z") 3]))))
|
||||
|
||||
(for/sum ([line (in-list input)])
|
||||
(+ 1 (second line)
|
||||
(~> (- (second line) 2 (first line))
|
||||
(modulo 3)
|
||||
(* 3)))))
|
||||
|
||||
(define (part2 input)
|
||||
(for/sum ([x (in-list input)])
|
||||
(match-define (list x1 x2) x)
|
||||
(+ (match x2
|
||||
["X" 0]
|
||||
["Y" 3]
|
||||
["Z" 6])
|
||||
(match* (x1 x2)
|
||||
[("A" "X") 3]
|
||||
[("A" "Y") 1]
|
||||
[("A" "Z") 2]
|
||||
[("B" "X") 1]
|
||||
[("B" "Y") 2]
|
||||
[("B" "Z") 3]
|
||||
[("C" "X") 2]
|
||||
[("C" "Y") 3]
|
||||
[("C" "Z") 1]))))
|
||||
(for/sum ([line (in-list input)])
|
||||
(+ (* (second line) 3)
|
||||
(~> (+ (second line) (first line) 2)
|
||||
(modulo 3)
|
||||
(add1)))))
|
||||
|
||||
(define (parse fname)
|
||||
(for/list ([x (in-list (file->lines fname))])
|
||||
(string-split x " ")))
|
||||
|
||||
(module+ test
|
||||
(require rackunit)
|
||||
(displayln "no tests :("))
|
||||
(for/list ([line (in-list (file->lines fname))])
|
||||
(map #{hash-ref (hash "A" 0 "B" 1 "C" 2 "X" 0 "Y" 1 "Z" 2) %}
|
||||
(string-split line " "))))
|
||||
|
||||
(module+ main
|
||||
(define input (parse "inputs/2"))
|
||||
|
|
Loading…
Reference in New Issue