-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0808-soup-servings.rkt
51 lines (46 loc) · 1.64 KB
/
0808-soup-servings.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
43
44
45
46
47
48
49
50
51
#lang racket
(define (iter h)
(for/fold ([nh (make-hash)])
([(res pos) h])
(cond [(and (positive? (car res))
(positive? (cdr res)))
(let ([updater (lambda (x) (+ x (/ pos 4)))])
(begin (hash-update! nh
(cons (- (car res) 100)
(cdr res))
updater 0)
(hash-update! nh
(cons (- (car res) 75)
(- (cdr res) 25))
updater 0)
(hash-update! nh
(cons (- (car res) 50)
(- (cdr res) 50))
updater 0)
(hash-update! nh
(cons (- (car res) 25)
(- (cdr res) 75))
updater 0))
nh)]
[(and (positive? (car res))
(<= (cdr res) 0))
nh]
[else
(hash-set! nh res pos)
nh])))
(define/contract (soup-servings n)
(-> exact-integer? flonum?)
(if (> n 4801)
1.0
(let ([fh (for/fold ([h (hash (cons n n) 1.0)])
([i (in-range (add1 (/ n 50)))])
(iter h))])
(for/sum ([(res pos) fh])
(if (positive? (cdr res))
pos
(/ pos 2))))))
(define n 1)
(iter (hash (cons n n) 1.0))
(iter (iter (hash (cons n n) 1.0)))
(iter (iter (iter (hash (cons n n) 1.0))))
(soup-servings n)