Last active
October 14, 2015 00:05
-
-
Save bryangarza/6b92a3ce7889adfeee13 to your computer and use it in GitHub Desktop.
Revisions
-
bryangarza revised this gist
Oct 14, 2015 . 1 changed file with 11 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,12 +26,11 @@ ;; carry :: State -> Move -> (State | '()) (define carry (λ (state change) (match-let* ([(list m c b) state] [(list m-delta c-delta) change] [(list move-op new-b) (if b (list - #f) (list + #t))] [new-state (list (move-op m m-delta) (move-op c c-delta) new-b)]) (if (valid? new-state) new-state null)))) ;; only 5 possible state changes: @@ -47,11 +46,15 @@ ['() #f] [_ (let* ([choice (car choices)] [new-state (carry state choice)]) (if (not (null? new-state)) new-state (next-level state (cdr choices))))]))) ;; bfs :: State -> State (define bfs (λ (state) (let* ([b (third state)] [res (next-level state (if b state-change-right state-change-left))]) (if (or (not res) (equal? res goal-state)) (list res) (cons state (bfs res)))))) -
bryangarza revised this gist
Oct 14, 2015 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,13 +26,12 @@ ;; carry :: State -> Move -> (State | '()) (define carry (λ (state change) (match-let* ([(list m c b) state] [(list m-delta c-delta) change] [(list move-op new-b) (if b (list - #f) (list + #t))] [new-state (list (move-op m m-delta) (move-op c c-delta) new-b)]) (if (valid? new-state) new-state null)))) ;; only 5 possible state changes: -
bryangarza revised this gist
Oct 13, 2015 . 1 changed file with 8 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,17 +26,14 @@ ;; carry :: State -> Move -> (State | '()) (define carry (λ (state change) (match-let* ([(list m c b) state] [(list m-delta c-delta) change] [move-op (if b - +)] [new-m (move-op m m-delta)] [new-c (move-op c c-delta)] [new-b (if b #f #t)] [new-state (list new-m new-c new-b)]) (if (valid? new-state) new-state null)))) ;; only 5 possible state changes: ;; state-change-{right,left} :: [(Missionaries, Cannibals)] -
bryangarza revised this gist
Oct 13, 2015 . 1 changed file with 15 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,20 @@ #lang racket (require racket/match) ;; initial-state :: State ;; State = (Missionaries on left, Cannibals on left, Boat on left?) (define initial-state '(3 3 #t)) ;; goal-state :: State (define goal-state '(0 0 #f)) ;; valid :: State -> Bool (define valid? (λ (state) (let* ([m (car state)] [m-r (- 3 m)] [c (second state)] [c-r (- 3 c)]) (and (or (= m 0) (>= m c)) (or (= m-r 0) (>= m-r c-r)) @@ -31,29 +31,31 @@ [c (second state)] [c-delta (second change)] [b (third state)] [move-op (if b - +)] [new-m (move-op m m-delta)] [new-c (move-op c c-delta)] [new-b (if b #f #t)] [new-state (list new-m new-c new-b)]) (if (valid? new-state) new-state null)))) ;; only 5 possible state changes: ;; state-change-{right,left} :: [(Missionaries, Cannibals)] (define state-change-right '((2 0) (0 2) (1 1) (1 0) (0 1))) (define state-change-left '((0 1) (1 0) (1 1) (0 2) (2 0))) ;; try all 5 possibilities, or at least until reaching a next state ;; next-level :: State -> (State | #f) (define next-level (λ (state choices) (match choices ['() #f] [_ (let* ([choice (car choices)] [new-state (carry state choice)]) (if (not (null? new-state)) new-state (next-level state (cdr choices))))]))) ;; bfs :: State -> State (define bfs (λ (state) (let* ([b (third state)] [res (next-level state (if b state-change-right state-change-left))]) (if (or (not res) (equal? res goal-state)) (list res) (cons state (bfs res)))))) -
bryangarza revised this gist
Oct 13, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,9 +28,9 @@ (λ (state change) (let* ([m (car state)] [m-delta (car change)] [c (second state)] [c-delta (second change)] [b (third state)] [move-op (if (= b 0) - +)] [new-m (move-op m m-delta)] [new-c (move-op c c-delta)] -
bryangarza created this gist
Oct 13, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ #lang racket (require racket/match) (require racket/trace) ;; initial-state :: State (define initial-state '(3 3 0)) ;; goal-state :: State (define goal-state '(0 0 1)) ;; valid :: State -> Bool (define valid? (λ (state) (let* ([m (car state)] [m-r (- 3 m)] [c (second state)] [c-r (- 3 c)]) (and (or (= m 0) (>= m c)) (or (= m-r 0) (>= m-r c-r)) (<= m 3) (<= c 3) (>= m 0) (>= c 0))))) ;; carry :: State -> Move -> (State | '()) (define carry (λ (state change) (let* ([m (car state)] [m-delta (car change)] [c (car (cdr state))] [c-delta (car (cdr change))] [b (car (cdr (cdr state)))] [move-op (if (= b 0) - +)] [new-m (move-op m m-delta)] [new-c (move-op c c-delta)] [new-b (if (= b 0) 1 0)] [new-state (list new-m new-c new-b)]) (if (valid? new-state) new-state null)))) ;; only 5 possible state changes: (define state-change-right '((2 0) (0 2) (1 1) (1 0) (0 1))) (define state-change-left '((0 1) (1 0) (1 1) (0 2) (2 0))) ;; next-level :: State -> State ;; try all 5 possibilities, or at least until reaching a next state (define next-level (λ (state choices) (match choices [(list) #f] [_ (let* ([choice (car choices)] [new-state (carry state choice)]) (if (not (null? new-state)) new-state (next-level state (cdr choices))))]))) (define bfs (λ (state) (let* ([b (third state)] [res (next-level state (if (= b 0) state-change-right state-change-left))]) (if (or (not res) (equal? res goal-state)) res (bfs res)))))