44 lines
1.4 KiB
Racket
44 lines
1.4 KiB
Racket
|
#lang curly-fn racket
|
||
|
|
||
|
(require "scripts/aoc.rkt")
|
||
|
|
||
|
;; solution for day 2
|
||
|
|
||
|
(define (part1 input)
|
||
|
(define max-red 12)
|
||
|
(define max-green 13)
|
||
|
(define max-blue 14)
|
||
|
(for/sum ([game (in-list input)] [game-id (in-naturals 1)])
|
||
|
(define possible?
|
||
|
(for/and ([rnd (in-list game)])
|
||
|
(and (<= (hash-ref rnd 'red 0) max-red)
|
||
|
(<= (hash-ref rnd 'green 0) max-green)
|
||
|
(<= (hash-ref rnd 'blue 0) max-blue))))
|
||
|
(if possible? game-id 0)))
|
||
|
|
||
|
|
||
|
(define (part2 input)
|
||
|
(for/sum ([game (in-list input)])
|
||
|
(define min-red (apply max (map #{hash-ref % 'red 0} game)))
|
||
|
(define min-green (apply max (map #{hash-ref % 'green 0} game)))
|
||
|
(define min-blue (apply max (map #{hash-ref % 'blue 0} game)))
|
||
|
(* min-red min-green min-blue)))
|
||
|
|
||
|
(define (parse fname)
|
||
|
(define input (file->lines fname))
|
||
|
(for/list ([line (in-list input)])
|
||
|
(match-define (pregexp #px"Game ([0-9]+): (.*)" (list _ game entries)) line)
|
||
|
(define rounds (string-split entries ";"))
|
||
|
(for/list ([rnd (in-list rounds)])
|
||
|
(define items (string-split rnd ","))
|
||
|
(for/hash ([item (in-list items)])
|
||
|
(match-define (pregexp #px"([0-9]+) (red|green|blue)"
|
||
|
(list _ (app string->number cnt) (app string->symbol color))) item)
|
||
|
(values color cnt)))))
|
||
|
|
||
|
(module+ main
|
||
|
(define input (parse "inputs/2"))
|
||
|
(answer 2 1 (time (part1 input)))
|
||
|
(answer 2 2 (time (part2 input)))
|
||
|
(displayln "meow"))
|