Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module GumimazeAgent
- open Microsoft.FSharp.Control
- open System.Threading.Tasks
- type ReducerMessage = Point of int | End
- type CounterMessage = Inc | Dec
- type WalkerMessage = Map<int*int, char> * (int * int) * Map<int*int, bool> * int;
- let answer = new TaskCompletionSource<int>()
- let reducer = MailboxProcessor<_>.Start(fun mb ->
- let rec mainLoop max = async {
- let! message = mb.Receive()
- match message with
- | Point pt -> return! mainLoop (if pt > max then pt else max)
- | _ -> return answer.SetResult(max)
- }
- mainLoop 0
- )
- let counter = MailboxProcessor<_>.Start(fun mb ->
- let rec mainLoop count = async {
- let! message = mb.Receive()
- let count =
- match message with
- | Inc -> count + 1
- | Dec -> count - 1
- if count > 0 then
- return! mainLoop count
- else
- End |> reducer.Post(ReducerMessage.End)
- }
- mainLoop 0
- )
- let rec walkerBody = fun (mb: MailboxProcessor<_>) -> async {
- let! msg = mb.Receive()
- let walked = Map.add msg.Position true msg.Walked
- let aroundP =
- let x, y = msg.Position
- [x+1, y; x, y+1; x-1, y; x, y-1]
- for p in aroundP do
- match Map.containsKey p walked, Map.find p msg.Maze with
- | true, _ -> ()
- | _ , 'W' -> ()
- | _ , '1' ->
- Inc |> counter.Post
- let walker = MailboxProcessor<_>.Start(walkerBody)
- (msg.Maze, p, walked, msg.Point + 1) |> walker.Post
- | _ , 'G' ->
- Point msg.Point |> reducer.Post
- | _ , _ ->
- Inc |> counter.Post
- let walker = MailboxProcessor<_>.Start(walkerBody)
- (msg.Maze, p, walked, msg.Point) |> walker.Post
- Dec |> counter.Post
- }
- let walker = MailboxProcessor<_>.Start(walkerBody)
- let withIndex xs =
- Array.mapi (fun i x -> x, i) xs
- let read =
- let lines = "maze.txt" |> System.IO.File.ReadAllLines
- seq {
- for line, y in withIndex lines do
- for c, x in withIndex (line.ToCharArray ()) ->
- (x, y), c
- }
- |> Map.ofSeq
- let solve maze =
- let p = maze |> Map.pick (fun k v -> if v = 'S' then Some k else None)
- Inc |> counter.Post
- (maze, p, Map.empty, 0) |> walker.Post
- [<EntryPoint>]
- let main argv =
- read |> solve
- answer.Task.Result |> printfn "%A"
- 0
Add Comment
Please, Sign In to add comment