1 %% to compile: erlc day3A.erl
2 %% to run: erl -noshell -s day5 solve
6 -export ([solve/0, solve/1, solve/2]).
7 -export ([read_input/0]).
15 solve(A, read_input()).
18 io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
22 io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
27 {ok, IO} = file:open("input.txt", 'read'),
28 Data = read_input(IO),
36 case file:read_line(IO) of
38 {ok, Line} -> parse_line(Line)
42 Points = string:tokens(Line, " ,->\n"),
43 parse_line([list_to_integer(X) || X <- Points], [{0,0},{1,0},{2,0},{3,0},{4,0},{5,0},{6,0},{7,0},{8,0}]).
45 parse_line([], Acc) ->
46 io:format("input: ~p~n", [Acc]),
48 parse_line([H|T], Acc) ->
49 {A, V} = lists:keyfind(H, 1, Acc),
50 parse_line(T, lists:keyreplace(H, 1, Acc, {A, V + 1})).
53 Fish = days(Input, 80),
54 io:format("OUT: ~p~n", [Fish]),
55 lists:foldl(fun({_A, V}, Acc) -> Acc + V end, 0, Fish).
57 Fish = days(Input, 256),
58 io:format("OUT: ~p~n", [Fish]),
59 lists:foldl(fun({_A, V}, Acc) -> Acc + V end, 0, Fish).
64 lists:foldl(fun({A, V}, AccIn) ->
67 {7, V1} = lists:keyfind(7, 1, Input),
68 AccIn ++ [{6, V + V1},{8, V}];
70 _ -> AccIn ++ [{A-1, V}]