-
Notifications
You must be signed in to change notification settings - Fork 11
/
kattis_falsesecurity.ml
104 lines (97 loc) · 2.25 KB
/
kattis_falsesecurity.ml
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
(*Kattis - falsesecurity
Relatively fine, alot of manipulation between string and char list in this question unforunately.
*)
open Printf
let charToMorse = function
| 'A' -> ".-"
| 'B' -> "-..."
| 'C' -> "-.-."
| 'D' -> "-.."
| 'E' -> "."
| 'F' -> "..-."
| 'G' -> "--."
| 'H' -> "...."
| 'I' -> ".."
| 'J' -> ".---"
| 'K' -> "-.-"
| 'L' -> ".-.."
| 'M' -> "--"
| 'N' -> "-."
| 'O' -> "---"
| 'P' -> ".--."
| 'Q' -> "--.-"
| 'R' -> ".-."
| 'S' -> "..."
| 'T' -> "-"
| 'U' -> "..-"
| 'V' -> "...-"
| 'W' -> ".--"
| 'X' -> "-..-"
| 'Y' -> "-.--"
| 'Z' -> "--.."
| '_' -> "..--"
| ',' -> ".-.-"
| '.' -> "---."
| '?' -> "----"
| _ -> failwith "Invalid character"
let morseToChar = function
|".-" -> 'A'
|"-..." -> 'B'
|"-.-." -> 'C'
|"-.." -> 'D'
|"." -> 'E'
|"..-." -> 'F'
|"--." -> 'G'
|"...." -> 'H'
|".." -> 'I'
|".---" -> 'J'
|"-.-" -> 'K'
|".-.." -> 'L'
|"--" -> 'M'
|"-." -> 'N'
|"---" -> 'O'
|".--." -> 'P'
|"--.-" -> 'Q'
|".-." -> 'R'
|"..." -> 'S'
|"-" -> 'T'
|"..-" -> 'U'
|"...-" -> 'V'
|".--" -> 'W'
|"-..-" -> 'X'
|"-.--" -> 'Y'
|"--.." -> 'Z'
|"..--" -> '_'
|".-.-" -> ','
|"---." -> '.'
|"----" -> '?'
| _ -> failwith "Invalid morse code"
let rec take n xs = match (n, xs) with
| (0, _) -> []
| (_, []) -> []
| (n, x::xs) -> x :: take (n-1) xs
let rec drop n xs = match (n, xs) with
| (0, _) -> xs
| (_, []) -> []
| (n, x::xs) -> drop (n-1) xs
let rec splitBy xs ys = match (xs, ys) with
| ([], _) -> []
| (x::xs, ys) -> take x ys :: splitBy xs (drop x ys)
let string_of_chars chars =
let buf = Buffer.create 16 in
List.iter (Buffer.add_char buf) chars;
Buffer.contents buf
let rec solve() =
try
let message = read_line () in
let morseList = List.map charToMorse (List.of_seq (String.to_seq message)) in
let lengthList = List.map String.length morseList in
let revLengthList = List.rev lengthList in
let outcome = splitBy revLengthList (List.of_seq (String.to_seq (List.fold_right (^) morseList "" ))) in
let outcome2 = List.map (fun zs -> morseToChar (string_of_chars(zs))) outcome in
let outcome3 = string_of_chars outcome2 in
printf "%s\n" outcome3;
solve()
with
End_of_file -> ()
let () = solve()