-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrankAndFile.erl
More file actions
55 lines (45 loc) · 1.62 KB
/
Copy pathrankAndFile.erl
File metadata and controls
55 lines (45 loc) · 1.62 KB
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
-module(rankAndFile).
-export([read_and_write/0]).
read_and_write() ->
{ok, FileDesc} = file:open("input_B.txt", read),
{Iter, _} = string:to_integer(io:get_line(FileDesc, '')),
Results = compute(Iter, FileDesc),
file:close(FileDesc),
{ok, OutFd} = file:open("output_B.txt", write),
write_file(OutFd, Results, 1),
file:close(OutFd).
compute(Iter, FDesc) ->
compute_outer_loop(Iter, FDesc, []).
compute_outer_loop(0, _File, Acc) -> lists:reverse(Acc);
compute_outer_loop(Iter, File, Acc) ->
{Num, _} = string:to_integer(io:get_line(File, '')),
Result = inner_loop(2*Num-1, File, maps:new()),
compute_outer_loop(Iter-1, File, [Result | Acc]).
inner_loop(0, _File, Map) ->
List = maps:to_list(Map),
Unsorted = [Key || {Key, Value} <- List, Value rem 2 =/= 0],
lists:sort(Unsorted);
inner_loop(Iter, File, Map) ->
Tokens = string:tokens(string:strip(io:get_line(File, ''), right, $\n), " "),
Map2 = add_or_insert(Tokens, Map),
inner_loop(Iter-1, File, Map2).
add_or_insert([], Map) -> Map;
add_or_insert([H | T], Map) ->
{Key, _} = string:to_integer(H),
Map2 = case maps:is_key(Key, Map) of
false -> maps:put(Key, 1, Map);
true ->
Value = maps:get(Key, Map),
maps:update(Key, Value+1, Map)
end,
add_or_insert(T, Map2).
write_file(_File, [], _Count) -> ok;
write_file(File, [H | T], Count) ->
io:format(File, "Case #~w: ", [Count]),
print_list(H, File),
write_file(File, T, Count+1).
print_list([], File) ->
io:format(File, "~n", []);
print_list([H|T], File) ->
io:format(File, "~w ", [H]),
print_list(T, File).