0 people like it.
Like the snippet!
Darkus April-2012 contest entry
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
|
open System.IO
let start = System.DateTime.Now
let alphas =
['а';'б';'в';'г';'д';'е';'ё';'ж';'з';'и';
'й';'к';'л';'м';'н';'о';'п';'р';'с';'т';
'у';'ф';'х';'ц';'ч';'ш';'щ';'ъ';'ы';'ь';
'э';'ю';'я']
let nums = [0..32]
let ht = System.Collections.Generic.Dictionary<char,int>()
List.zip alphas nums |> List.iter (fun (k,v) -> ht.[k] <- v)
let encodeFromString (s: string) =
let rec f i n =
if i = 4 then n
else
let c = ht.[s.[i]]
f (i+1) (n*33+c)
f 0 0
let decodeToString (v: int) =
let a = Array.create<char> 4 ' '
let rec f i n =
if i = -1 then System.String(a)
else
a.[i] <- List.nth alphas (n%33)
f (i-1) (n/33)
f 3 v
let reader =
seq { use reader = StreamReader(File.OpenRead @"I:\Lang\Ocaml\ssp\Vocabulary-4.txt")
while not reader.EndOfStream do
let s = reader.ReadLine()
if s.Length = 4 then
let r = encodeFromString s
if (decodeToString r) <> s then
failwith (sprintf "%s <> %s" (decodeToString r) s)
yield encodeFromString s }
//reader |> Seq.iter (fun v -> printfn "%d" v);;
let words = Array.ofSeq reader
let wordWithout w = function
|0 -> w%(33*33*33)
|1 -> (w/33/33/33)*33*33*33+w%(33*33)
|2 -> (w/33/33)*33*33+w%33
|_ -> (w/33)*33
let ht0 = System.Collections.Generic.Dictionary<int,Set<int>>()
let ht1 = System.Collections.Generic.Dictionary<int,Set<int>>()
let ht2 = System.Collections.Generic.Dictionary<int,Set<int>>()
let ht3 = System.Collections.Generic.Dictionary<int,Set<int>>()
let hs = [| ht0;ht1;ht2;ht3 |]
let initWWO v =
for i in 0..3 do
let k = wordWithout v i
let h = hs.[i]
if h.ContainsKey k then h.[k] <- h.[k].Add(v)
else h.Add(k, Set.singleton v)
words |> Array.iter (fun w -> initWWO w)
//printfn "0: %d 1: %d 2: %d 3: %d" ht0.Count ht1.Count ht2.Count ht3.Count
exception Found of string*int
exception NotFound
let find maxSteps srcWord dstWord =
let steps = ref 0
let srcNum = encodeFromString srcWord
let dstNum = encodeFromString dstWord
let prevSrc = ref Set.empty<int>
let curSrc = ref (Set.singleton srcNum)
try
let f = ref true
while !steps <> words.Length && (Set.count !prevSrc) <> (Set.count !curSrc) do
incr steps
prevSrc := !curSrc
curSrc := Set.empty<int>
for v in !prevSrc do
for i in 0..3 do
let k = wordWithout v i
let h = hs.[i]
if h.ContainsKey k then
if Set.contains dstNum h.[k] then
let prevWord = decodeToString v
raise (Found(prevWord,i))
curSrc := Set.union !curSrc (Set.difference h.[k] !prevSrc)
curSrc := Set.union !prevSrc !curSrc
raise NotFound
with
| Found(prev,k) ->
//printfn "found %s at %d step over %s where any is %d" (decodeToString dstNum) !steps prev k
prev,!steps
| NotFound ->
// failwith (sprintf "not found for an %d steps and %d words" !steps (Set.count !prevSrc))
"",-1
let paths src dst =
let word, steps = find words.Length src dst
if steps = -1 then
printfn "%s -> %s (нет шагов): -" src dst
else
let results = ref [word; dst]
let maxSteps = ref steps
while src <> (List.head !results) do
let word, steps = find !maxSteps src (List.head !results)
results := word::!results
maxSteps := steps
printf "%s -> %s (%d шагов): " src dst (List.length !results)
!results |> List.iter (fun v -> printf "%s " v)
printf "\n"
let srcList = ["муха";"день";"снег";"отец";"рука";"зима";"свет";"липа"]
let dstList = ["слон";"ночь";"вода";"мать";"нога";"лето";"тьма";"клён"]
List.zip srcList dstList |> List.iter (fun (src,dst) -> paths src dst)
let stop = System.DateTime.Now
let ts = stop-start
printfn "at %A" ts
|
namespace System
namespace System.IO
val start : System.DateTime
Full name: Script.start
Multiple items
type DateTime =
struct
new : ticks:int64 -> DateTime + 10 overloads
member Add : value:TimeSpan -> DateTime
member AddDays : value:float -> DateTime
member AddHours : value:float -> DateTime
member AddMilliseconds : value:float -> DateTime
member AddMinutes : value:float -> DateTime
member AddMonths : months:int -> DateTime
member AddSeconds : value:float -> DateTime
member AddTicks : value:int64 -> DateTime
member AddYears : value:int -> DateTime
...
end
Full name: System.DateTime
--------------------
System.DateTime()
(+0 other overloads)
System.DateTime(ticks: int64) : unit
(+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
(+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
(+0 other overloads)
property System.DateTime.Now: System.DateTime
val alphas : char list
Full name: Script.alphas
val nums : int list
Full name: Script.nums
val ht : System.Collections.Generic.Dictionary<char,int>
Full name: Script.ht
namespace System.Collections
namespace System.Collections.Generic
Multiple items
type Dictionary<'TKey,'TValue> =
new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
member Add : key:'TKey * value:'TValue -> unit
member Clear : unit -> unit
member Comparer : IEqualityComparer<'TKey>
member ContainsKey : key:'TKey -> bool
member ContainsValue : value:'TValue -> bool
member Count : int
member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
member Item : 'TKey -> 'TValue with get, set
...
nested type Enumerator
nested type KeyCollection
nested type ValueCollection
Full name: System.Collections.Generic.Dictionary<_,_>
--------------------
System.Collections.Generic.Dictionary() : unit
System.Collections.Generic.Dictionary(capacity: int) : unit
System.Collections.Generic.Dictionary(comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
System.Collections.Generic.Dictionary(dictionary: System.Collections.Generic.IDictionary<'TKey,'TValue>) : unit
System.Collections.Generic.Dictionary(capacity: int, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
System.Collections.Generic.Dictionary(dictionary: System.Collections.Generic.IDictionary<'TKey,'TValue>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
Multiple items
val char : value:'T -> char (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.char
--------------------
type char = System.Char
Full name: Microsoft.FSharp.Core.char
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val zip : list1:'T1 list -> list2:'T2 list -> ('T1 * 'T2) list
Full name: Microsoft.FSharp.Collections.List.zip
val iter : action:('T -> unit) -> list:'T list -> unit
Full name: Microsoft.FSharp.Collections.List.iter
val k : char
val v : int
val encodeFromString : s:string -> int
Full name: Script.encodeFromString
val s : string
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
val f : (int -> int -> int)
val i : int
val n : int
val c : int
val decodeToString : v:int -> System.String
Full name: Script.decodeToString
val a : char []
module Array
from Microsoft.FSharp.Collections
val create : count:int -> value:'T -> 'T []
Full name: Microsoft.FSharp.Collections.Array.create
val f : (int -> int -> System.String)
Multiple items
type String =
new : value:char -> string + 7 overloads
member Chars : int -> char
member Clone : unit -> obj
member CompareTo : value:obj -> int + 1 overload
member Contains : value:string -> bool
member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
member EndsWith : value:string -> bool + 2 overloads
member Equals : obj:obj -> bool + 2 overloads
member GetEnumerator : unit -> CharEnumerator
member GetHashCode : unit -> int
...
Full name: System.String
--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit
val nth : list:'T list -> index:int -> 'T
Full name: Microsoft.FSharp.Collections.List.nth
val reader : seq<int>
Full name: Script.reader
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val reader : StreamReader
Multiple items
type StreamReader =
inherit TextReader
new : stream:Stream -> StreamReader + 9 overloads
member BaseStream : Stream
member Close : unit -> unit
member CurrentEncoding : Encoding
member DiscardBufferedData : unit -> unit
member EndOfStream : bool
member Peek : unit -> int
member Read : unit -> int + 1 overload
member ReadLine : unit -> string
member ReadToEnd : unit -> string
...
Full name: System.IO.StreamReader
--------------------
StreamReader(stream: Stream) : unit
StreamReader(path: string) : unit
StreamReader(stream: Stream, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(stream: Stream, encoding: System.Text.Encoding) : unit
StreamReader(path: string, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(path: string, encoding: System.Text.Encoding) : unit
StreamReader(stream: Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(stream: Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
type File =
static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
static member AppendAllText : path:string * contents:string -> unit + 1 overload
static member AppendText : path:string -> StreamWriter
static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
static member Create : path:string -> FileStream + 3 overloads
static member CreateText : path:string -> StreamWriter
static member Decrypt : path:string -> unit
static member Delete : path:string -> unit
static member Encrypt : path:string -> unit
static member Exists : path:string -> bool
...
Full name: System.IO.File
File.OpenRead(path: string) : FileStream
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
property StreamReader.EndOfStream: bool
StreamReader.ReadLine() : string
property System.String.Length: int
val r : int
val failwith : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.failwith
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val words : int []
Full name: Script.words
val ofSeq : source:seq<'T> -> 'T []
Full name: Microsoft.FSharp.Collections.Array.ofSeq
val wordWithout : w:int -> _arg1:int -> int
Full name: Script.wordWithout
val w : int
val ht0 : System.Collections.Generic.Dictionary<int,Set<int>>
Full name: Script.ht0
Multiple items
module Set
from Microsoft.FSharp.Collections
--------------------
type Set<'T (requires comparison)> =
interface IComparable
interface IEnumerable
interface IEnumerable<'T>
interface ICollection<'T>
new : elements:seq<'T> -> Set<'T>
member Add : value:'T -> Set<'T>
member Contains : value:'T -> bool
override Equals : obj -> bool
member IsProperSubsetOf : otherSet:Set<'T> -> bool
member IsProperSupersetOf : otherSet:Set<'T> -> bool
...
Full name: Microsoft.FSharp.Collections.Set<_>
--------------------
new : elements:seq<'T> -> Set<'T>
val ht1 : System.Collections.Generic.Dictionary<int,Set<int>>
Full name: Script.ht1
val ht2 : System.Collections.Generic.Dictionary<int,Set<int>>
Full name: Script.ht2
val ht3 : System.Collections.Generic.Dictionary<int,Set<int>>
Full name: Script.ht3
val hs : System.Collections.Generic.Dictionary<int,Set<int>> []
Full name: Script.hs
val initWWO : v:int -> unit
Full name: Script.initWWO
val i : int32
val k : int
val h : System.Collections.Generic.Dictionary<int,Set<int>>
System.Collections.Generic.Dictionary.ContainsKey(key: int) : bool
System.Collections.Generic.Dictionary.Add(key: int, value: Set<int>) : unit
val singleton : value:'T -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.singleton
val iter : action:('T -> unit) -> array:'T [] -> unit
Full name: Microsoft.FSharp.Collections.Array.iter
exception Found of string * int
Full name: Script.Found
exception NotFound
Full name: Script.NotFound
val find : maxSteps:'a -> srcWord:string -> dstWord:string -> string * int
Full name: Script.find
val maxSteps : 'a
val srcWord : string
val dstWord : string
val steps : int ref
Multiple items
val ref : value:'T -> 'T ref
Full name: Microsoft.FSharp.Core.Operators.ref
--------------------
type 'T ref = Ref<'T>
Full name: Microsoft.FSharp.Core.ref<_>
val srcNum : int
val dstNum : int
val prevSrc : Set<int> ref
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.empty
val curSrc : Set<int> ref
val f : bool ref
property System.Array.Length: int
val count : set:Set<'T> -> int (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.count
val incr : cell:int ref -> unit
Full name: Microsoft.FSharp.Core.Operators.incr
val contains : element:'T -> set:Set<'T> -> bool (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.contains
val prevWord : System.String
val raise : exn:System.Exception -> 'T
Full name: Microsoft.FSharp.Core.Operators.raise
val union : set1:Set<'T> -> set2:Set<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.union
val difference : set1:Set<'T> -> set2:Set<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.difference
val prev : string
val paths : src:string -> dst:string -> unit
Full name: Script.paths
val src : string
val dst : string
val word : string
val steps : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val results : string list ref
val maxSteps : int ref
val head : list:'T list -> 'T
Full name: Microsoft.FSharp.Collections.List.head
val printf : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val length : list:'T list -> int
Full name: Microsoft.FSharp.Collections.List.length
val v : string
val srcList : string list
Full name: Script.srcList
val dstList : string list
Full name: Script.dstList
val stop : System.DateTime
Full name: Script.stop
val ts : System.TimeSpan
Full name: Script.ts
More information