Demo entry 6731097

racket test

   

Submitted by anonymous on Apr 10, 2018 at 11:03
Language: Racket. Code size: 1.3 kB.

;;-----------------------------------------------------------------------------
;; [2018-01-22] Challenge #348 [Easy] 
;; The rabbit problem
;; 
;; Tetsumi <tetsumi@protonmail.com>
;;
;; Compatible with
;;   RACKET
;;-----------------------------------------------------------------------------
#lang racket

(define (months males females needed)
  (define pMonths (make-vector 92 '(0 . 0)))
  (define-syntax-rule (vpMonths i) (vector-ref pMonths i))
  (vector-set! pMonths 89 (cons females males))
  (let loop ([months 1]
             [breeders 0]
             [total (+ females males)]
             [deaths 0]) 
    (if (> total  needed)
        (cons months deaths)
        (let* ([i (modulo months 92)]
               [pDeaths (vector-ref pMonths i)]
               [cBreed (- breeders (car pDeaths))]
               [females (* 9 cBreed)]
               [males (* 5 cBreed)])
          (vector-set! pMonths i (cons females males))
          (loop (add1 months)
                (+ cBreed (car (vector-ref pMonths (modulo (- months 4) 92))))
                (+ females males (- total (cdr pDeaths)))
                (+ (car pDeaths) (cdr pDeaths) deaths))))))
               

(for ([e (in-port)])
  (let ([ans (months e (read) (read))])
    (printf "~a months, ~a deaths~%" (car ans) (cdr ans))))

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).