Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.reddit.com/r/dailyprogrammer/comments/2uo3yf/20150204_challenge_200_intermediate_metro_tile/
- type point = {x:int;y:int}
- type tile = {position : point; width : int; height : int}
- let width, height = (fun (s : string array) -> (int(s.[0]), int(s.[1]))) (System.Console.ReadLine().Split(' '))
- let boardchrs = ([for i in 1 .. height do yield System.Console.ReadLine()] |> List.fold (fun str line -> str + line) "").ToCharArray()
- let board = Array2D.create height width ('.')
- for x in 0..height - 1 do
- for y in 0..width - 1 do
- Array2D.set board x y (boardchrs.[x*width + y])
- let rec checktiles pos tiles =
- match tiles with
- | h :: t -> if (h.position.x <= pos.x && pos.x <= h.position.x + h.width - 1) && (h.position.y <= pos.y && pos.y <= h.position.y + h.height - 1) then true else checktiles pos t
- | _ -> false
- let incpos (pos : point) =
- match pos with
- | {x = x; y = y} when y = height - 1 && x = width - 1 -> pos
- | {x = x; y = y} when y < height - 1 -> {pos with y = y + 1}
- | {x = x; y = y} when y = height - 1 -> {x = x + 1; y = 0}
- | _ -> printfn "This should never happen"; pos
- let findtile pos =
- let rec findwidth pos currwidth =
- if pos.x = width then currwidth
- elif board.[pos.y, pos.x] <> '.' then findwidth {pos with x = pos.x + 1} (currwidth + 1)
- else currwidth
- let rec findheight pos currheight =
- if pos.y = height then currheight
- elif board.[pos.y, pos.x] <> '.' then findheight {pos with y = pos.y + 1} (currheight + 1)
- else currheight
- {position = pos; width = findwidth pos 0; height = findheight pos 0}
- let rec scanboard pos tiles =
- if board.[pos.y, pos.x] <> '.' && not(checktiles pos tiles) then scanboard (incpos pos) ((findtile pos) :: tiles)
- elif incpos pos <> pos then scanboard (incpos pos) tiles
- else tiles
- let tiles = scanboard {x = 0; y = 0} []
- tiles |> List.rev |> List.iter (fun t -> printfn "%dx%d tile of character '%c' located at (%d,%d)" t.width t.height board.[t.position.y, t.position.x] t.position.x t.position.y)
Add Comment
Please, Sign In to add comment