# Advent of Code 2021 - Day 5

**URL:** <https://erlangforums.com/t/advent-of-code-2021-day-5/738>\
**Category:** Coding Challenges\
**Tags:** advent-of-code\
**Created:** [December 5, 2021, 4:59am UTC](https://erlangforums.com/t/advent-of-code-2021-day-5/738 "2021-12-05T04:59:18Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![phild](https://erlangforums.com/user_avatar/erlangforums.com/phild/32/742_2.png) [@phild](https://erlangforums.com/u/phild)\
**Post date:** [December 5, 2021, 1:25pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-5/738/4 "2021-12-05T13:25:36Z")

</div>

> [@danilagamma](#):
>
> This one feels easier than previous day

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](https://erlangforums.com/t/which-code-editor-or-ide-do-you-use/463).

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`):

```erlang
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.

```

---

_[View the full topic](https://erlangforums.com/t/advent-of-code-2021-day-5/738)._
