-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem2017_01.erl
37 lines (27 loc) · 1.06 KB
/
problem2017_01.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
-module(problem2017_01).
-export([solve1/1, solve2/1]).
-spec solve( nonempty_string(), nonempty_string() ) -> integer().
solve( Input, ShiftedInput ) ->
F = fun ( A, A ) -> erlang:list_to_integer( [ A ] );
( _, _ ) -> 0
end,
lists:sum( lists:zipwith( F, Input, ShiftedInput ) ).
-spec solve1( nonempty_string() ) -> integer().
solve1( Input ) ->
solve( Input, tl( Input ) ++ [ hd( Input ) ] ).
-spec solve2( nonempty_string() ) -> integer().
solve2( Input ) ->
{ L2, L1 } = lists:split( length( Input ) div 2, Input ),
solve( Input, L1 ++ L2 ).
-include_lib("eunit/include/eunit.hrl").
solve1_test_() ->
[ ?_assertEqual( 3 , solve1( "1122" ) ),
?_assertEqual( 4 , solve1( "1111" ) ),
?_assertEqual( 0 , solve1( "1234" ) ),
?_assertEqual( 9 , solve1( "91212129" ) ) ].
solve2_test_() ->
[ ?_assertEqual( 6, solve2( "1212" ) ),
?_assertEqual( 0, solve2( "1221" ) ),
?_assertEqual( 4, solve2( "123425" ) ),
?_assertEqual( 12, solve2( "123123" ) ),
?_assertEqual( 4, solve2( "12131415" ) ) ].