Last active
February 22, 2017 14:32
-
-
Save damonmcminn/5efcf4bac3c55b81bb32ec60a53b126c to your computer and use it in GitHub Desktop.
Goldilocks
This file contains hidden or 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
| Sample input seat positions: | |
| "2 5" | |
| Actual input seat positions: | |
| "1 3 11 15 17 19 22 23 26" |
This file contains hidden or 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
| type Weight = Weight of int | |
| type Temperature = Temperature of int | |
| type Capacity = Capacity of Weight | |
| type Goldilocks = { | |
| Weight:Weight | |
| MaxTemp:Temperature | |
| } | |
| type Chair = { | |
| Capacity:Capacity | |
| PorridgeTemperature:Temperature | |
| Position:int | |
| } | |
| module Input = | |
| let typify (w, t) = Weight w, Temperature t | |
| let toGoldilocks (w, t) = { Weight = w; MaxTemp = t } | |
| let toChair p (w, t) = { Capacity = Capacity w; PorridgeTemperature = t; Position = p } | |
| let findSeats inputs = | |
| let goldilocks = (List.head >> Input.toGoldilocks) inputs | |
| let maxWeight = Capacity goldilocks.Weight | |
| let toChair i vals = | |
| let position = i + 1 | |
| Input.toChair position vals | |
| let isGoldilocksChair (chair:Chair) = | |
| chair.Capacity >= maxWeight && chair.PorridgeTemperature <= goldilocks.MaxTemp | |
| inputs | |
| |> List.tail | |
| |> List.mapi toChair | |
| |> List.filter isGoldilocksChair | |
| let printSeatPositions inputs = | |
| inputs | |
| |> List.map Input.typify | |
| |> findSeats | |
| |> Seq.map (fun chair -> (chair.Position.ToString())) | |
| |> Seq.reduce (sprintf "%s %s") | |
| |> (printfn "%A") | |
| printfn "Sample input seat positions:" | |
| [ | |
| 100, 80 | |
| 30, 50 | |
| 130, 75 | |
| 90, 60 | |
| 150, 85 | |
| 120, 70 | |
| 200, 200 | |
| 110, 100 | |
| ] |> printSeatPositions | |
| printfn "Actual input seat positions:" | |
| [ | |
| 100, 120 | |
| 297, 90 | |
| 66, 110 | |
| 257, 113 | |
| 276, 191 | |
| 280, 129 | |
| 219, 163 | |
| 254, 193 | |
| 86, 153 | |
| 206, 147 | |
| 71, 137 | |
| 104, 40 | |
| 238, 127 | |
| 52, 146 | |
| 129, 197 | |
| 144, 59 | |
| 157, 124 | |
| 210, 59 | |
| 11, 54 | |
| 268, 119 | |
| 261, 121 | |
| 12, 189 | |
| 186, 108 | |
| 174, 21 | |
| 77, 18 | |
| 54, 90 | |
| 174, 52 | |
| 16, 129 | |
| 59, 181 | |
| 290, 123 | |
| 248, 132 | |
| ] |> printSeatPositions |
This file contains hidden or 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 | |
| ;; defines types | |
| (struct chair (capacity porridge-temp position)) | |
| (struct goldilocks (weight max-temp)) | |
| ;; defines creation functions for readability | |
| ;; will call goldilocks struct with list of args | |
| ;; this is to avoid having to unnecessarily define each arg to call the struct with | |
| ;; i.e. for each list in inputs, the list would have to be "unpacked" to get the arguments | |
| ;; currying apply avoids this for us | |
| (define ->goldilocks ((curry apply) goldilocks)) | |
| (define (->chair args index) | |
| (let ([position (+ 1 index)]) | |
| (chair (first args) (second args) position))) | |
| ;; maps list elements of type "1 2" to a list of elements of type '(1 2) | |
| (define (numberify inputs) | |
| (map (lambda (str) | |
| (define nums (string-split str " ")) | |
| (map (lambda (n) (string->number n)) nums)) inputs)) | |
| ;; creates predicate function that determines if a chair is suitable for goldilocks | |
| (define (good-chair?-creator g) | |
| (lambda (c) | |
| (and (>= (chair-capacity c) (goldilocks-weight g)) | |
| (<= (chair-porridge-temp c) (goldilocks-max-temp g))))) | |
| ;; couldn't find how to iterate with index... | |
| (define (mapi fn xs) | |
| (for/list ([index (in-range (length xs))]) | |
| (let ([current-item (list-ref xs index)]) | |
| (fn current-item index)))) | |
| ;; returns chair list | |
| (define (get-good-chairs inputs) | |
| (let ([good-chair? (good-chair?-creator (->goldilocks (first inputs)))] | |
| [chairs (mapi ->chair (rest inputs))]) | |
| (filter good-chair? chairs))) | |
| ;; generates output string as "expected" by challenge | |
| ;; e.g.: "1 2" if chairs in position 1 and 2 are good chairs | |
| (define (create-output-string xs) | |
| (let ([good-chairs (get-good-chairs xs)]) | |
| (string-trim ;; my format string causes an unnecessary leading space | |
| (foldl (lambda (chair result) | |
| (format "~a ~a" result (chair-position chair))) | |
| "" good-chairs)))) | |
| (define (get-inputs lines) | |
| (let ([line (read-line)]) | |
| (if (eof-object? line) | |
| lines | |
| (get-inputs (append lines (list line)))))) | |
| (println (create-output-string (numberify (get-inputs '())))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment