-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.rkt
42 lines (37 loc) · 1.35 KB
/
day7.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#lang racket
(define (add-bag bags desc)
(let ([color (regexp-match #rx"^[a-z]+ [a-z]+" desc)]
[inside (regexp-match*
#rx"([0-9]+) ([a-z]+ [a-z]+) (?=bags\\.|bag\\.|bags\\, |bag\\, )"
desc
#:match-select cdr)])
(hash-set! bags (car color) inside)))
(define (contains-shiny-gold bags ls)
(for/fold ([cnt 0])
([bag ls])
(let* ([color (cadr bag)]
[inside (hash-ref bags color)])
(cond [(equal? color "shiny gold") (+ cnt 1)]
[(eq? 0 (length inside)) cnt]
[else (+ cnt (contains-shiny-gold bags inside))]))))
(define (solve1 bags)
(for/fold ([cnt 0])
([(color inside) (in-hash bags)])
(+ cnt (if (< 0 (contains-shiny-gold bags inside))
1
0))))
(define (solve2 bags inside)
(cond [(eq? 0 (length inside)) 0]
[(eq? 1 (length inside))
(+ (string->number (caar inside))
(* (string->number (caar inside))
(solve2 bags (hash-ref bags (cadr (car inside))))))]
[else (+ (solve2 bags (cons (car inside) '()))
(solve2 bags (cdr inside)))]))
(call-with-input-file "day7_in.txt"
(lambda (f)
(let ([bags (make-hash)])
(for ([l (in-lines f)])
(add-bag bags l))
(solve1 bags)
(- (solve2 bags '(("1" "shiny gold"))) 1))))