# Advent of Code 2021 - Day 5

This topic is about Day 5 of the Advent of Code 2021.

We have a private leaderboard (shared with users of the elixir forum):

The entry code is:
`370884-a6a71927`

5 Likes

This one feels easier than previous day :

``````main(File) ->
Data = [ begin
[From, To] = binary:split(Line, <<" -> ">>),
[X1b, Y1b] = binary:split(From, <<",">>),
[X2b, Y2b] = binary:split(To,   <<",">>),
{{binary_to_integer(X1b), binary_to_integer(Y1b)},
{binary_to_integer(X2b), binary_to_integer(Y2b)}}
end || Line <- binary:split(RawData, <<"\n">>, [global, trim]) ],
io:format("part 1: ~p~n", [solve1(Data)]),
io:format("part 2: ~p~n", [solve2(Data)]),
ok.

solve1(Data) ->
count_overlap([ L || Coords <- Data,
L <- to_straight_line(Coords) ]).

count_overlap(Lines) ->
Map = lists:foldl(fun update_counter/2, #{}, Lines),
length([ X || X <- maps:values(Map), X >= 2 ]).

update_counter(Coord, Acc) ->
Fun = fun(V) -> V + 1 end,
maps:update_with(Coord, Fun, 1, Acc).

to_straight_line({{X1, Y1}, {X2, Y2}}) when X1 =:= X2 ->
[ {X1, Y} || Y <- from_to(Y1, Y2) ];
to_straight_line({{X1, Y1}, {X2, Y2}}) when Y1 =:= Y2 ->
[ {X, Y1} || X <- from_to(X1, X2) ];
to_straight_line(_) ->
[].

from_to(A, B) ->
case B > A of
true  -> lists:seq(A, B);
false -> lists:seq(A, B, -1)
end.

solve2(Data) ->
count_overlap([ L || Coords <- Data,
L <- to_straight_line(Coords)
++ to_diagonal_line(Coords) ]).

to_diagonal_line({{X1, Y1}, {X2, Y2}}) when abs(X1 - X2) =:= abs(Y1 - Y2) ->
lists:zip(from_to(X1, X2), from_to(Y1, Y2));
to_diagonal_line(_) ->
[].
``````

Curious to see solution without keeping each individual dot in memory with counter

5 Likes

Day 5 in Sesterl - adventofcode/main.sest at master Â· michallepicki/adventofcode Â· GitHub
All those `if then else` are getting ugly, I should send a PR to add `&&` and `||`

4 Likes

Yes, or perhaps just well suited to Erlang. And unlike yesterday, itâ€™s not a test of how efficiently you can write a file parser or manipulate text in your favourite editor.

I feel happy enough with my code today to post it (although having seen it, I like @ danilagammaâ€™s `from_to/2` better than my `dir/2` - though Iâ€™d implement it as clauses rather than a `case`):

``````p1(InputData) ->
count_points(fun points_p1/2, InputData).

p2(InputData) ->
count_points(fun points_p2/2, InputData).

count_points(F, InputData) ->
AllPoints = lists:foldl(
fun(E, A) ->
maps:put(E, maps:get(E, A, 0) + 1, A)
end,
#{},
lists:flatten([F(StartPos, EndPos) || {StartPos, EndPos} <- InputData])),
maps:size(maps:filter(fun(_K, V) -> V >= 2 end, AllPoints)).

points_p1({X1, Y}, {X2, Y}) ->
[{X, Y} || X <- lists:seq(X1, X2, dir(X1, X2))];
points_p1({X, Y1}, {X, Y2}) ->
[{X, Y} || Y <- lists:seq(Y1, Y2, dir(Y1, Y2))];
points_p1(_, _) ->
[].

points_p2({X1, Y1}, {X2, Y2}) when abs(X2 - X1) =:= abs(Y2 - Y1) ->
Xs = [X || X <- lists:seq(X1, X2, dir(X1, X2))],
Ys = [Y || Y <- lists:seq(Y1, Y2, dir(Y1, Y2))],
lists:zip(Xs, Ys);
points_p2(P1, P2) ->
points_p1(P1, P2).

dir(A, B) when A < B ->
1;
dir(_, _) ->
-1.
``````
6 Likes

As an aside, it feels like `maps:put(Key, maps:get(Key, Map, 0) + 1, Map)` is a common enough idiom that it should have its own library function. Is it me thatâ€™s missing something, or is OTP?

4 Likes

Thereâ€™s `maps:update_with/4`

6 Likes

My lazy solution for â€śHydrothermal Ventureâ€ť - Day 5 - Advent of Code 2021 in Erlang.

4 Likes

Beautiful!
I learned that you can use begin and end on list comprehensions!

4 Likes

Compact version for p2:

``````-module(day5).
-export([main/1]).
-mode(compile).

main(_) ->
Rows = [[binary_to_integer(B) || B <- re:split(Line, "[,> -]+")]
|| Line <- string:split(Bin, "\n", all), Line =/= <<>>],
Tid = ets:new(acc, [set]),
[ets:update_counter(Tid, {X1+N*X, Y1+N*Y}, {2,1}, {{X1+N*X, Y1+N*Y},0})
|| [X1,Y1,X2,Y2] <- Rows,
X1==X2 orelse Y1==Y2 orelse abs(X2-X1)==abs(Y2-Y1),
X <- [if X1==X2 -> 0; true -> trunc((X2-X1)/abs(X2-X1)) end],
Y <- [if Y1==Y2 -> 0; true -> trunc((Y2-Y1)/abs(Y2-Y1)) end],
N <- lists:seq(0, max(abs(X2-X1),abs(Y2-Y1)))],
io:format("~p~n", [length([1 || {_,X} <- ets:tab2list(Tid), X > 1])]).
``````
8 Likes

Great! I was trying to find how to do that with string:split but I quit.

3 Likes
``````p5_1() ->
Hv = lists:append([ line(L, hv) || L <- p5_read("priv/p05.txt") ]),
R1 = length(lists:usort(lists:sort(Hv) -- lists:usort(Hv))),
D = lists:append([ line(L, diag) || L <- p5_read("priv/p05.txt") ]),
R2 = length(lists:usort(lists:sort(D) -- lists:usort(D))),
{R1, R2}.

line([[X,Y1],[X,Y2]], _) ->
[[X,Y] || Y <- seq(Y1,Y2)];
line([[X1,Y],[X2,Y]], _) ->
[[X,Y] || X <- seq(X1,X2)];
line([[X1,Y1], [X2,Y2]], diag) ->
[tuple_to_list(E)
|| E <- (lists:zip(seq(X1, X2), seq(Y1, Y2)))];
line(_, hv) ->
[].

seq(A,B) when A < B ->
lists:seq(A,B);
seq(A,B) when A >= B ->
lists:seq(A,B,-1).

Lines = string:split(string:trim(Bin), "\n", all),
deep_integer([ [ string:split(P, ",", all)
|| P <-string:split(L, " -> ", all) ] || L <- Lines]) .
``````

With this re-used from yesterday:

``````deep_integer(L) when is_list(L) ->
[ deep_integer(E) || E <- L ];
deep_integer(B) when is_binary(B) ->
binary_to_integer(B).
``````
6 Likes
``````part1() ->
Data = data(re:split(Bin, "[,> -/\n]+")),
length(lists:usort(unique(lists:sort(lists:flatten(lines(Data)))))).

data([])                 -> [];
data([H1, H2, H3, H4|T]) -> [{{data(H1), data(H2)}, {data(H3), data(H4)}} | data(T)];
data([_|T])              -> data(T);
data(Bin)                -> binary_to_integer(Bin).

lines([])                     -> [];
lines([{{X, Y1}, {X, Y2}}|T]) -> [[{X, Y} || Y <- seq(Y1, Y2)] | lines(T)];
lines([{{X1, Y}, {X2, Y}}|T]) -> [[{X, Y} || X <- seq(X1, X2)] | lines(T)];
lines([_|T])                  -> lines(T).

unique([])       -> [];
unique([H, H|T]) -> [H | unique(T)];
unique([_|T])    -> unique(T).

seq(A, B) when A < B  -> lists:seq(A, B);
seq(A, B) when A >= B -> lists:seq(A, B, -1).
``````
5 Likes