<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://lorwiki.zhbert.ru/index.php?action=history&amp;feed=atom&amp;title=LOR-contest%2FOcaml</id>
	<title>LOR-contest/Ocaml - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://lorwiki.zhbert.ru/index.php?action=history&amp;feed=atom&amp;title=LOR-contest%2FOcaml"/>
	<link rel="alternate" type="text/html" href="http://lorwiki.zhbert.ru/index.php?title=LOR-contest/Ocaml&amp;action=history"/>
	<updated>2026-10-12T23:41:59Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.39.0</generator>
	<entry>
		<id>http://lorwiki.zhbert.ru/index.php?title=LOR-contest/Ocaml&amp;diff=107&amp;oldid=prev</id>
		<title>Admin: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://lorwiki.zhbert.ru/index.php?title=LOR-contest/Ocaml&amp;diff=107&amp;oldid=prev"/>
		<updated>2023-02-27T18:39:15Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 21:39, 27 февраля 2023&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>http://lorwiki.zhbert.ru/index.php?title=LOR-contest/Ocaml&amp;diff=106&amp;oldid=prev</id>
		<title>lor&gt;Cetjs2: Cetjs2 moved page LOR-contest:Ocaml to LOR-contest/Ocaml without leaving a redirect</title>
		<link rel="alternate" type="text/html" href="http://lorwiki.zhbert.ru/index.php?title=LOR-contest/Ocaml&amp;diff=106&amp;oldid=prev"/>
		<updated>2018-02-01T19:55:32Z</updated>

		<summary type="html">&lt;p&gt;Cetjs2 moved page &lt;a href=&quot;/index.php?title=LOR-contest:Ocaml&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;LOR-contest:Ocaml (страница не существует)&quot;&gt;LOR-contest:Ocaml&lt;/a&gt; to &lt;a href=&quot;/index.php/LOR-contest/Ocaml&quot; title=&quot;LOR-contest/Ocaml&quot;&gt;LOR-contest/Ocaml&lt;/a&gt; without leaving a redirect&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Программа состоит из четырех файлов: adv_world.ml, advserver.ml, hclient.ml, roboclient.ml. Кроме этого, еще два файла, которые не вошли в зачет -- adv_world.mli и Makefile.&lt;br /&gt;
&lt;br /&gt;
===[adv_world.ml]===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let find map c =&lt;br /&gt;
  let rec find_aux i =&lt;br /&gt;
    if i = Array.length map then raise Not_found&lt;br /&gt;
    else try (i, String.index map.(i) c) with Not_found -&amp;gt; find_aux (i + 1)&lt;br /&gt;
  in find_aux 0&lt;br /&gt;
&lt;br /&gt;
let get_items map =&lt;br /&gt;
  let rec get_items_aux accu = function&lt;br /&gt;
      (i, j) when i = Array.length map      -&amp;gt; accu&lt;br /&gt;
    | (i, j) when j = String.length map.(i) -&amp;gt; get_items_aux accu (i + 1, 0)&lt;br /&gt;
    | (i, j) -&amp;gt; get_items_aux (if map.(i).[j] = '$' then (i, j) :: accu else accu) (i, j + 1)&lt;br /&gt;
  in&lt;br /&gt;
  get_items_aux [] (0, 0) ;;&lt;br /&gt;
&lt;br /&gt;
let num_of_items map = List.length (get_items map) ;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===[advserver.ml]===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let serve_cmd map ((x, y), is_empty) = function&lt;br /&gt;
    &amp;quot;pickup&amp;quot; when is_empty &amp;amp;&amp;amp; map.(x).[y] = '$' -&amp;gt;&lt;br /&gt;
      map.(x).[y] &amp;lt;- ' ' ;&lt;br /&gt;
      ((x, y), false)&lt;br /&gt;
  | &amp;quot;drop&amp;quot; when not is_empty &amp;amp;&amp;amp; map.(x).[y] &amp;lt;&amp;gt; '$' -&amp;gt;&lt;br /&gt;
      if map.(x).[y] &amp;lt;&amp;gt; '.' then map.(x).[y] &amp;lt;- '$' ;&lt;br /&gt;
      ((x, y), true)&lt;br /&gt;
  | cmd -&amp;gt;&lt;br /&gt;
      let cmds = [(&amp;quot;north&amp;quot;, (-1, 0)); (&amp;quot;south&amp;quot;, (1, 0)); (&amp;quot;west&amp;quot;, (0, -1)); (&amp;quot;east&amp;quot;, (0, 1))] in&lt;br /&gt;
      let x', y' = (function (dx, dy) -&amp;gt; x + dx, y + dy) (try List.assoc cmd cmds with Not_found -&amp;gt; 0, 0) in&lt;br /&gt;
      ( if (try ignore map.(x).[y]; false with Invalid_argument _ -&amp;gt; true) || map.(x').[y'] = '#' then&lt;br /&gt;
          x, y else x', y' ), is_empty&lt;br /&gt;
&lt;br /&gt;
let send_stat out_channel map ((x, y), is_empty) =&lt;br /&gt;
  let map' = Array.map (function line -&amp;gt; String.copy line) map in&lt;br /&gt;
  map'.(x).[y] &amp;lt;- '@' ;&lt;br /&gt;
  Marshal.to_channel out_channel map' [] ;&lt;br /&gt;
  Marshal.to_channel out_channel&lt;br /&gt;
    [ if Adv_world.num_of_items map = 0 &amp;amp;&amp;amp; is_empty then &amp;quot;You won!&amp;quot; else &amp;quot;Game is in progress&amp;quot;;&lt;br /&gt;
      if is_empty then &amp;quot;Your hand are empty&amp;quot; else &amp;quot;You carry an item&amp;quot;;&lt;br /&gt;
      if map.(x).[y] = '$' then &amp;quot;Here lies 1 item&amp;quot; else &amp;quot;Here lies 0 items&amp;quot; ] [] ;&lt;br /&gt;
  flush out_channel&lt;br /&gt;
&lt;br /&gt;
let rec input ic =&lt;br /&gt;
  try let line = input_line ic in line :: input ic with End_of_file -&amp;gt; []&lt;br /&gt;
&lt;br /&gt;
let serve in_channel out_channel =&lt;br /&gt;
  let map = Array.of_list (input (open_in Sys.argv.(1))) in&lt;br /&gt;
  let rec loop state =&lt;br /&gt;
    send_stat out_channel map state ;&lt;br /&gt;
    try loop (serve_cmd map state (input_line in_channel)) with&lt;br /&gt;
      End_of_file -&amp;gt; ()&lt;br /&gt;
  in&lt;br /&gt;
  loop ((Adv_world.find map '.'), true) ;;&lt;br /&gt;
&lt;br /&gt;
Unix.establish_server serve (Unix.ADDR_INET (Unix.inet_addr_loopback, int_of_string Sys.argv.(2))) ;; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===[hclient.ml]===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let in_channel, out_channel = Unix.open_connection&lt;br /&gt;
  (Unix.ADDR_INET ((Unix.inet_addr_of_string Sys.argv.(1)), int_of_string Sys.argv.(2)))&lt;br /&gt;
in&lt;br /&gt;
while true do&lt;br /&gt;
  Array.iter (Printf.printf &amp;quot;%s\n&amp;quot;) (Marshal.from_channel in_channel : string array) ;&lt;br /&gt;
  List.iter (Printf.printf &amp;quot;%s\n&amp;quot;) (Marshal.from_channel in_channel : string list) ;&lt;br /&gt;
  flush stdout ;&lt;br /&gt;
  Printf.fprintf out_channel &amp;quot;%s\n&amp;quot; (read_line ()) ;&lt;br /&gt;
  flush out_channel&lt;br /&gt;
done ;; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[roboclient.ml]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let dfs map (sx, sy) =&lt;br /&gt;
  let dxy = [(-1, 0); (1, 0); (0, -1); (0, 1)] in&lt;br /&gt;
  let f = Array.init&lt;br /&gt;
    (Array.length map) (function i -&amp;gt; Array.make (String.length map.(i)) (-1))&lt;br /&gt;
  in&lt;br /&gt;
  let q = Queue.create () in&lt;br /&gt;
  Queue.push (sx, sy) q ; f.(sx).(sy) &amp;lt;- 0 ;&lt;br /&gt;
  while not (Queue.is_empty q) do&lt;br /&gt;
    let x, y = Queue.pop q in&lt;br /&gt;
    List.iter&lt;br /&gt;
      (function (x', y') -&amp;gt; Queue.push (x', y') q ; f.(x').(y') &amp;lt;- f.(x).(y) + 1)&lt;br /&gt;
      ( List.filter&lt;br /&gt;
          ( function (x', y') -&amp;gt;&lt;br /&gt;
              (try ignore map.(x').[y']; true with Invalid_argument _ -&amp;gt; false) &amp;amp;&amp;amp;&lt;br /&gt;
                map.(x').[y'] &amp;lt;&amp;gt; '#' &amp;amp;&amp;amp; f.(x').(y') = -1 )&lt;br /&gt;
          (List.map (function (dx, dy) -&amp;gt; x + dx, y + dy) dxy) )&lt;br /&gt;
  done ;&lt;br /&gt;
  f&lt;br /&gt;
&lt;br /&gt;
let find_path f (x, y) =&lt;br /&gt;
  let rec find_path' (x, y) path rev_path =&lt;br /&gt;
    let dxy = [ ((-1, 0), (&amp;quot;south&amp;quot;, &amp;quot;north&amp;quot;)); ((1, 0), (&amp;quot;north&amp;quot;, &amp;quot;south&amp;quot;));&lt;br /&gt;
                ((0, -1), (&amp;quot;east&amp;quot;, &amp;quot;west&amp;quot;)); ((0, 1), (&amp;quot;west&amp;quot;, &amp;quot;east&amp;quot;)) ] in&lt;br /&gt;
    if f.(x).(y) = 0 then path @ [&amp;quot;pickup&amp;quot;] @ (List.rev rev_path) @ [&amp;quot;drop&amp;quot;] else&lt;br /&gt;
      let find_path_aux (dx, dy) (dir, rev_dir) =&lt;br /&gt;
        if try f.(x + dx).(y + dy) + 1 = f.(x).(y) with Invalid_argument _ -&amp;gt; false then&lt;br /&gt;
          find_path' (x + dx, y + dy) (dir :: path) (rev_dir :: rev_path)&lt;br /&gt;
        else []&lt;br /&gt;
      in&lt;br /&gt;
      List.fold_right&lt;br /&gt;
        (function (d, dirs) -&amp;gt; (function [] -&amp;gt; find_path_aux d dirs | path -&amp;gt; path)) dxy []&lt;br /&gt;
  in&lt;br /&gt;
  find_path' (x, y) [] [] ;;&lt;br /&gt;
&lt;br /&gt;
let in_channel, out_channel = Unix.open_connection&lt;br /&gt;
  (Unix.ADDR_INET ((Unix.inet_addr_of_string Sys.argv.(1)), int_of_string Sys.argv.(2)))&lt;br /&gt;
in&lt;br /&gt;
&lt;br /&gt;
let map = (Marshal.from_channel in_channel : string array) in&lt;br /&gt;
Array.iter (Printf.printf &amp;quot;%s\n&amp;quot;) map ;&lt;br /&gt;
List.iter (Printf.printf &amp;quot;%s\n&amp;quot;) (Marshal.from_channel in_channel : string list) ;&lt;br /&gt;
&lt;br /&gt;
List.iter&lt;br /&gt;
  ( function (x, y) -&amp;gt;&lt;br /&gt;
      List.iter&lt;br /&gt;
        ( function cmd -&amp;gt;&lt;br /&gt;
            Printf.fprintf out_channel &amp;quot;%s\n&amp;quot; cmd ;&lt;br /&gt;
            flush out_channel ;&lt;br /&gt;
            Array.iter (Printf.printf &amp;quot;%s\n&amp;quot;) (Marshal.from_channel in_channel : string array) ;&lt;br /&gt;
            List.iter (Printf.printf &amp;quot;%s\n&amp;quot;) (Marshal.from_channel in_channel : string list) )&lt;br /&gt;
        (find_path (dfs map (Adv_world.find map '@')) (x, y)) )&lt;br /&gt;
  ( Adv_world.get_items map ) ;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===[adv_world.mli]===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
val find : string array -&amp;gt; char -&amp;gt; int * int&lt;br /&gt;
val get_items : string array -&amp;gt; (int * int) list&lt;br /&gt;
val num_of_items : string array -&amp;gt; int&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===[Makefile]===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
adventure: advserver hclient roboclient&lt;br /&gt;
&lt;br /&gt;
advserver: adv_world.cmo advserver.ml&lt;br /&gt;
	ocamlc unix.cma adv_world.cmo advserver.ml -o advserver&lt;br /&gt;
hclient: adv_world.cmo hclient.ml&lt;br /&gt;
	ocamlc -g unix.cma adv_world.cmo hclient.ml -o hclient&lt;br /&gt;
roboclient: adv_world.cmo roboclient.ml&lt;br /&gt;
	ocamlc -g unix.cma adv_world.cmo roboclient.ml -o roboclient&lt;br /&gt;
adv_world.cmo: adv_world.mli adv_world.ml&lt;br /&gt;
	ocamlc -g -c adv_world.mli&lt;br /&gt;
	ocamlc -g -c adv_world.ml&lt;br /&gt;
&lt;br /&gt;
clean:&lt;br /&gt;
	rm -Rf *~ *.cm[io] advserver hclient roboclient&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:LOR-contest]]&lt;/div&gt;</summary>
		<author><name>lor&gt;Cetjs2</name></author>
	</entry>
</feed>