Created
March 20, 2019 20:28
-
-
Save default-kramer/073b68122ea047ac46b33af20cb1eba1 to your computer and use it in GitHub Desktop.
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
#lang racket | |
(require pict | |
rackunit) | |
; left and right are (or/c choice? team?) | |
; winner is (or/c #f team?) | |
(struct choice (left right winner) #:transparent) | |
(struct team (name strength) #:transparent) | |
(define (outcome? x) | |
(and (choice? x) | |
(choice-winner x))) | |
(define/contract (choose c) | |
(-> choice? outcome?) | |
(define (resolve x) | |
(cond | |
[(team? x) x] | |
[(outcome? x) x] | |
[else (choose x)])) | |
(define (get-team x) | |
(if (team? x) | |
x | |
(choice-winner x))) | |
(define left (resolve (choice-left c))) | |
(define right (resolve (choice-right c))) | |
(define left-team (get-team left)) | |
(define right-team (get-team right)) | |
; If team X has strength 2 and team Y has strengh 5, | |
; then X has a 2/7 chance to win (Y has 5/7). | |
(define left-str (team-strength left-team)) | |
(define right-str (team-strength right-team)) | |
(define total-str (+ left-str right-str)) | |
(define left-wins? (< (random) | |
(/ left-str total-str))) | |
(define winner (if left-wins? | |
left-team | |
right-team)) | |
(choice left right winner)) | |
; number of arguments must be a power of 2 | |
(define/contract (make-choices . choices) | |
(->* () #:rest (listof any/c) choice?) | |
(define (reducer lst) | |
(match lst | |
[(list a b rest ...) | |
(cons (choice a b #f) | |
(reducer rest))] | |
[(list) (list)])) | |
(define result (reducer choices)) | |
(match result | |
[(list a) a] | |
[(list rest ...) | |
(apply make-choices rest)])) | |
(check-equal? | |
(make-choices 1 2 3 4 5 6 7 8) | |
(choice (choice (choice 1 2 #f) | |
(choice 3 4 #f) #f) | |
(choice (choice 5 6 #f) | |
(choice 7 8 #f) #f) #f)) | |
(define (vs name1 str1 name2 str2) | |
(choice (team name1 (or str1 0.06)) | |
(team name2 (or str2 0.06)) #f)) | |
; Use "percent chance of winning the tournament" from 538 as team strength. | |
; This isn't the most accurate way, but it's good enough for me. | |
(define east | |
(make-choices | |
(vs "Duke" 19 | |
"NCC/NDAKST" #f) | |
(vs "VCU" #f | |
"UCF" #f) | |
(vs "Miss. State" 0.2 | |
"Liberty" #f) | |
(vs "Va. Tech" 2 | |
"Saint Louis" #f) | |
(vs "Maryland" 0.4 | |
"Belmont" #f) | |
(vs "LSU" 0.5 | |
"Yale" #f) | |
(vs "Louisville" 0.4 | |
"Minnesota" #f) | |
(vs "Michigan St." 7 | |
"Bradley" #f))) | |
(define west | |
(make-choices | |
(vs "Gonzaga" 15 | |
"F. Dickinson" #f) | |
(vs "Syracuse" 0.1 | |
"Baylor" #f) | |
(vs "Marquette" 0.2 | |
"Murray St." #f) | |
(vs "Florida St." 1 | |
"Vermont" #f) | |
(vs "Buffalo" 0.3 | |
"AZST/STJOHN" #f) | |
(vs "Texas Tech" 3 | |
"N. Kentucky" #f) | |
(vs "Nevada" 0.4 | |
"Florida" 0.1) | |
(vs "Michigan" 4 | |
"Montana" #f))) | |
(define south | |
(make-choices | |
(vs "Virginia" 17 | |
"G-Webb" #f) | |
(vs "Ole Miss" #f | |
"Oklahoma" #f) | |
(vs "Wisconson" 0.7 | |
"Oregon" #f) | |
(vs "Kansas St." 0.7 | |
"UC Irvine" #f) | |
(vs "Villanova" 0.7 | |
"Saint Mary's" #f) | |
(vs "Purdue" 2 | |
"Old Dominion" #f) | |
(vs "Cincinnati" 0.5 | |
"Iowa" #f) | |
(vs "Tennessee" 5 | |
"Colgate" #f))) | |
(define midwest | |
(make-choices | |
(vs "N. Carolina" 9 | |
"Iona" #f) | |
(vs "Utah St." #f | |
"Washington" #f) | |
(vs "Auburn" 2 | |
"N. Mex. St." #f) | |
(vs "Kansas" 1 | |
"Northeastern" #f) | |
(vs "Iowa St." 0.9 | |
"Ohio St." #f) | |
(vs "Houston" 1 | |
"Georgia St." #f) | |
(vs "Wofford" 0.2 | |
"Seton Hall" #f) | |
(vs "Kentucky" 5 | |
"Abilene Chr." #f))) | |
(define (show x [depth 10]) | |
;(-> (or/c team? outcome?) pict?) | |
(cond | |
[(= 0 depth) | |
(blank)] | |
[(team? x) | |
(lc-superimpose (rectangle 100 20) | |
(text (format " ~a" (team-name x))))] | |
[else | |
(hc-append (vl-append (show (choice-left x) (sub1 depth)) | |
(show (choice-right x) (sub1 depth))) | |
(show (choice-winner x)))])) | |
(define east-result (choose east)) | |
(define west-result (choose west)) | |
(define south-result (choose south)) | |
(define midwest-result (choose midwest)) | |
(displayln "east") | |
(show east-result) | |
(displayln "\n\nwest") | |
(show west-result) | |
(displayln "\n\nsouth") | |
(show south-result) | |
(displayln "\n\nmidwest") | |
(show midwest-result) | |
(define final-result | |
(choose (make-choices east-result | |
west-result | |
south-result | |
midwest-result))) | |
(displayln "\n\nfinal") | |
(show final-result 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment