# Advent of Code 2024 - Day 2

**URL:** <https://erlangforums.com/t/advent-of-code-2024-day-2/4240>\
**Category:** Coding Challenges\
**Tags:** advent-of-code\
**Created:** [December 2, 2024, 7:29am UTC](https://erlangforums.com/t/advent-of-code-2024-day-2/4240 "2024-12-02T07:29:21Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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, 2024, 3:33pm UTC](https://erlangforums.com/t/advent-of-code-2024-day-2/4240/7 "2024-12-05T15:33:34Z")

</div>

I admitted defeat on an elegant solution for part 2 and came here seeking enlightenment. Disappointed there’s nothing simple and clean.

Part 1, I was tolerably happy with:

```erlang
p1(InputData) ->
    length([Row || Row <- InputData, safe(util:str_to_ints(Row))]).

safe(L) ->
    safe(L, 0) =:= length(L) - 1.

safe([_H], A) -> abs(A);
safe([H | T], A) when A >= 0 andalso H < hd(T) andalso H >= hd(T) - 3 -> safe(T, A + 1);
safe([H | T], A) when A =< 0 andalso H > hd(T) andalso H =< hd(T) + 3 -> safe(T, A - 1);
safe(_, _) -> 0.

```

(See my [Day 1 post](https://erlangforums.com/t/advent-of-code-2024-day-1/4231/9) for utility & unit test functions).

Note that the incrementing/decrementing guards (`A >= 0` and `A =< 0` respectively) are actually superfluous, but would make it more efficient with longer input rows.

---

_[View the full topic](https://erlangforums.com/t/advent-of-code-2024-day-2/4240)._
