Execute SNUSP/OCaml: Difference between revisions

Content added Content deleted
m (→‎{{header|OCaml}}: added bounds)
Line 27: Line 27:
let ruld (x,y) = (y,x)
let ruld (x,y) = (y,x)
let lurd (x,y) = (-y,-x)
let lurd (x,y) = (-y,-x)

let next_pos ~pos:(x,y) ~dir:(dx,dy) = (x+dx, y+dy)


let incr (l,v,r) = (l,v+1,r)
let incr (l,v,r) = (l,v+1,r)
Line 45: Line 43:


let zero = function (_,0,_) -> true | _ -> false
let zero = function (_,0,_) -> true | _ -> false

let next_pos ~pos:(x,y) ~dir:(dx,dy) = (x+dx, y+dy)

let code = read Sys.argv.(1)
let max_x = Array.fold_left (fun v line -> max v (String.length line)) 0 code
let max_y = Array.length code


let get_char code (x,y) =
let get_char code (x,y) =
if x < 0 || y < 0 || x > max_x || y > max_y then raise Exit;
try code.(y).[x]
try code.(y).[x]
with _ -> '\000'
with _ -> '\000'


let () =
let () =
let code = read Sys.argv.(1) in
let rec loop pos dir cells stk =
let rec loop pos dir cells stk =
match (get_char code pos) with
match (get_char code pos) with