aboutsummaryrefslogtreecommitdiff
path: root/racket/leetcode/lc-1700-students-unable-to-eat.rkt
blob: 75cc24370c6399fc8b16085e3b45b376a0f99de5 (plain)
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
#lang racket
(define/contract (count-students students sandwiches)
  (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)
  (for/fold ([sandwich-pile sandwiches]
             [student-line students]
             [remaining-students (length students)]
             [break? #false]
             #:result remaining-students)
            ([i (in-naturals)]
             #:break break?)
    (cond [(and (empty? sandwich-pile)
                (empty? student-line))
           (values void
                   void
                   remaining-students
                   #true)]
          [(equal? (car sandwich-pile)
                   (car student-line))
           (values (cdr sandwich-pile)
                   (cdr student-line)
                   (sub1 remaining-students)
                   #false)]
          [(and (not (equal? (list (car sandwich-pile))
                        (remove-duplicates student-line)))
                (= 1 (length (remove-duplicates student-line))))
           (values void
                   void
                   remaining-students
                   #true)]
          [else
           (values sandwich-pile
                   (append (cdr student-line) (list (car student-line)))
                   remaining-students
                   #false)])))

(count-students '(1 1 0 0) '(0 1 0 1))