This commit is contained in:
2022-10-04 11:32:17 +00:00
parent 6110e75650
commit f62c52d8a9
8 changed files with 325 additions and 15 deletions

68
day17/day17.erl Normal file
View File

@@ -0,0 +1,68 @@
%% to compile: erlc day3A.erl
%% to run: erl -noshell -s day5 solve
%%
-module(day17).
-export ([solve/0, solve/1, solve/2]).
-compile ([export_all]).
solve() ->
solve(['1']),
solve(['2']),
init:stop().
solve(A) ->
solve(A, read_input()).
solve(['1'], D) ->
io:format("The solution to ~p puzzle1 is: ~p~n", [?MODULE, solve(1, D)]);
solve(1, D) ->
solution1(D);
solve(['2'], D) ->
io:format("The solution to ~p puzzle2 is: ~p~n", [?MODULE, solve(2, D)]);
solve(2, D) ->
solution2(D).
solution1({X,Y}) ->
Xs = find_x(X),
Ys = find_y(Y),
{T, X1, Y1} = lists:max([{Key, X1, proplists:get_value(Key,Ys)} || {Key, X1} <- Xs, lists:keysearch(Key, 1, Ys) /= false]),
y_max(T,Y1,0).
solution2({X,Y}) ->
Xs = find_x(X),
Ys = find_y(Y),
length(lists:usort([{X,Y} || {Ty, Y} <- Ys, {T,X} <- lists:filter(fun({T,X}) -> T == Ty end, Xs)])).
read_input() ->
{{124,174},{-123, -86}}.
%% {{20,30},{-10, -5}}.
find_x({Xmin, Xmax}) ->
[ {T, Xstart} || Xstart <- lists:seq(0,400), T <- lists:seq(1,1000), x(Xstart, T) >= Xmin, x(Xstart, T) =< Xmax ].
find_y({Ymin, Ymax}) ->
[ {T, Ystart} || Ystart <- lists:seq(-400,400), T <- lists:seq(1,1000), y(Ystart, T) >= Ymin, y(Ystart, T) =< Ymax ].
x(Xstart, T) ->
Last =
case (Xstart - T + 1) of
L when L < 0 -> 0;
L -> L
end,
(Xstart - Last + 1) * (Xstart + Last) div 2.
y(Ystart, T) ->
Last = (Ystart - T + 1),
(Ystart - Last + 1) * (Ystart + Last) div 2.
y_max(0, _, Ymax) -> Ymax;
y_max(T, Y, Ymax) ->
NewY = y(Y, T),
NewMax =
case NewY of
M when M > Ymax -> M;
_ -> Ymax
end,
y_max(T-1, Y, NewMax).

26
day17/diagram.txt Normal file
View File

@@ -0,0 +1,26 @@
1 2 3
4 0123456789012345678901234567890
3 .............#....#............
2 .......#..............#........
1 ...............................
0 S........................#.....
-1 ...............................
-2 ...............................
-3 ...........................#...
-4 ...............................
-5 ....................TTTTTTTTTTT
-6 ....................TTTTTTTTTTT
-7 ....................TTTTTTTT#TT
-8 ....................TTTTTTTTTTT
-9 ....................TTTTTTTTTTT
-10 ....................TTTTTTTTTTT
T X y
0 0 0 = 0
1 7 +6 = 7
2 13 +5 = 7 + 6
3 18 +4 = 7 + 6 + 5
4 22 +3 = 7 + 6 + 5 + 4
5 25 +2
6 27 +1
7 28 +0

1
day17/input.txt Normal file
View File

@@ -0,0 +1 @@
target area: x=124..174, y=-123..-86