Created
March 19, 2023 23:15
-
-
Save kamahen/720c1305258cd066ece24a315d1d0d24 to your computer and use it in GitHub Desktop.
ChatGPT writes Racket code to solve the visitor problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Define the hosts and visitors as symbols | |
(define hosts '(akira bhaskar charlemagne)) | |
(define visitors '(delancy erika fei-fei)) | |
; Define the availability of each host | |
(define availability | |
'((akira (1 2 3)) | |
(bhaskar (1 3 4)) | |
(charlemagne (2 4)))) | |
; Define the preferences of each visitor | |
(define preferences | |
'((delancy (akira charlemagne)) | |
(erika (akira bhaskar)) | |
(fei-fei (akira bhaskar charlemagne)))) | |
; Define a helper function to get the availability of a host | |
(define (get-availability host) | |
(cdr (assoc host availability))) | |
; Define a helper function to check if two time slots overlap | |
(define (time-overlaps? time1 time2) | |
(or (= time1 time2) (= time1 (+ time2 1)) (= time2 (+ time1 1)))) | |
; Define a helper function to check if a host is available at a certain time | |
(define (host-available? host time) | |
(member time (get-availability host))) | |
; Define a helper function to check if a visitor prefers a host | |
(define (visitor-prefers? visitor host) | |
(member host (cdr (assoc visitor preferences)))) | |
; Define a recursive function to match visitors with hosts | |
(define (match-visitors hosts visitors matches) | |
(cond | |
; Base case: all visitors have been matched | |
((null? visitors) matches) | |
; Recursive case: try to match the first visitor with a host | |
(else | |
(let* ((visitor (car visitors)) | |
(preferred-hosts (filter (lambda (host) (visitor-prefers? visitor host)) hosts)) | |
(matching-host (findf (lambda (host) | |
(and (host-available? host (car matches)) | |
(not (member host (cdr matches))) | |
(or (null? preferred-hosts) (member host preferred-hosts)))) | |
hosts))) | |
; If no matching host was found, backtrack | |
(if (null? matching-host) | |
(match-visitors hosts (cons visitor (cdr visitors)) (cdr matches)) | |
; Otherwise, add the match to the list and recurse | |
(match-visitors hosts (cdr visitors) (cons (cons matching-host (car matches)) (cdr matches)))))))) | |
; Call the matching function with the initial state | |
(match-visitors hosts visitors '(())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment