-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol06b.erl
executable file
·43 lines (36 loc) · 1.03 KB
/
sol06b.erl
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
#!/usr/bin/escript
main([]) ->
main(["sol06.data"]);
main([Filename]) ->
{ok, File} = file:open(Filename, [read]),
Data = parse_file(File),
file:close(File),
Counts = [count_universal_answers(G) || G <- Data],
Total = lists:foldl(fun erlang:'+'/2, 0, Counts),
io:format("~B~n", [Total]).
parse_file(File) ->
parse_line(File, read_line(File), [[]]).
parse_line(_File, eof, Acc) ->
Acc;
parse_line(File, "", Acc) ->
parse_line(File, read_line(File), [[]|Acc]);
parse_line(File, Line, [First | Rest]) ->
parse_line(File, read_line(File), [[Line | First] | Rest]).
read_line(File) ->
case file:read_line(File) of
{ok, Line} ->
string:chomp(Line);
Other ->
Other
end.
count_universal_answers(Group) ->
People = length(Group),
Answers = lists:sort(lists:append(Group)),
AnswersByQuestion = split_answers(Answers),
length([A || A <- AnswersByQuestion,
length(A) =:= People]).
split_answers([]) ->
[];
split_answers([X | _] = A) ->
{Group, Rest} = lists:splitwith(fun (E) -> E =:= X end, A),
[Group | split_answers(Rest)].