-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.erl
63 lines (54 loc) · 1.39 KB
/
solution.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%% https://www.hackerrank.com/challenges/botcleanr
-module(solution).
-export([main/0,stop/0]).
main()->solve(read_board()).
solve({N, Board})->
P=dirt_at({N, Board}),
M=me_at({N, Board}),
move(M, P)
.
move({My,_}, {Py,_}) when My < Py ->
io:format("DOWN~n");
move({My,_}, {Py,_}) when My > Py ->
io:format("UP~n");
move({_,Mx}, {_,Px}) when Mx < Px ->
io:format("RIGHT~n");
move({_,Mx}, {_,Px}) when Mx > Px ->
io:format("LEFT~n");
move(_, _)->io:format("CLEAN~n").
dirt_at({N, Board})->object_at({N, Board}, 2).
me_at({N, Board})->object_at({N, Board}, 1).
object_at({N, Board}, O)->
object_at({N, Board}, O, 1).
object_at({N, Board}, O, Y) when Y =< N->
X=string:str(lists:nth(Y, Board), [O]),
case X of
0->object_at({N, Board}, O, Y + 1);
_->{Y, X}
end;
object_at(_, _, _)->
false.
stop()->
halt().
read_board()->
{ok, [_]} = io:fread("", "~s"),
{ok, [_]} = io:fread("", "~s"),
read_board({5, []}, 1).
read_board({N, Board}, I) when I =< N ->
case io:fread("", "~s") of
{ok, [Line]}->read_board({N, Board++[interpret_line(Line)]}, I + 1)
end;
read_board({N, Board}, I) when I > N ->
{N, Board}.
interpret_line([], Col)->
Col;
interpret_line([H|Tail], Col)->
case H of
45->interpret_line(Tail, Col++[0]); %% -
98->interpret_line(Tail, Col++[1]); %% b
100->interpret_line(Tail, Col++[2]); %% d
_->io:format("~p~n", [H])
end.
interpret_line(Line)->
interpret_line(Line, [])
.