# Advent of Code 2021 - Day 1

**URL:** <https://erlangforums.com/t/advent-of-code-2021-day-1/710>\
**Category:** Coding Challenges\
**Tags:** advent-of-code\
**Created:** [December 1, 2021, 4:59am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710 "2021-12-01T04:59:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjorng](https://erlangforums.com/user_avatar/erlangforums.com/bjorng/32/174_2.png) [@bjorng](https://erlangforums.com/u/bjorng)\
**Post date:** [December 1, 2021, 4:59am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/1 "2021-12-01T04:59:35Z")

</div>

This topic is about Day 1 of the [Advent of Code 2021](https://adventofcode.com/2021).

We have a private leaderboard (shared with users of the [elixir forum](https://elixirforum.com)):

[https://adventofcode.com/2021/leaderboard/private/view/370884](https://adventofcode.com/2021/leaderboard/private/view/370884)

The entry code is:  
`370884-a6a71927`

---

<div class="post-metadata">

**Author:** ![bjorng](https://erlangforums.com/user_avatar/erlangforums.com/bjorng/32/174_2.png) [@bjorng](https://erlangforums.com/u/bjorng)\
**Post date:** [December 1, 2021, 5:38am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/4 "2021-12-01T05:38:50Z")

</div>

Here is my solution:

> <https://gist.github.com/bjorng/797744c41923db4f2f85d6b5afa226c9>

---

<div class="post-metadata">

**Author:** ![Gwaeron](https://erlangforums.com/user_avatar/erlangforums.com/gwaeron/32/833_2.png) [@Gwaeron](https://erlangforums.com/u/Gwaeron)\
**Post date:** [December 1, 2021, 6:06am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/5 "2021-12-01T06:06:03Z")

</div>

Spoiler alert! (Is it possible to mark replies as spoilers here?)

Omitting the file reading and type conversion, this is how I did it.

```erlang
part1([A, B | T]) when B > A -> 1 + part1([B | T]);                             
part1([_, B | T]) -> part1([B | T]);                                            
part1(_) -> 0.                                                                  
                                                                                
part2([A, B, C, D | T]) when B+C+D > A+B+C -> 1 + part2([B, C, D | T]);         
part2([_, B, C, D | T]) -> part2([B, C, D | T]);                                
part2(_) -> 0.

```

---

<div class="post-metadata">

**Author:** ![bjorng](https://erlangforums.com/user_avatar/erlangforums.com/bjorng/32/174_2.png) [@bjorng](https://erlangforums.com/u/bjorng)\
**Post date:** [December 1, 2021, 6:35am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/6 "2021-12-01T06:35:55Z")

</div>

> [@Gwaeron](#):
>
> Spoiler alert! (Is it possible to mark replies as spoilers here?)

I think that no spoiler markers are necessary, because it should be understood that every reply in an AoC thread is a spoiler.

---

<div class="post-metadata">

**Author:** ![vkatsuba](https://erlangforums.com/user_avatar/erlangforums.com/vkatsuba/32/314_2.png) [@vkatsuba](https://erlangforums.com/u/vkatsuba)\
**Post date:** [December 1, 2021, 10:25am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/7 "2021-12-01T10:25:10Z")

</div>

Cool! Let code challenge begin! 🤘

```erlang
%% part1(Input, 0).
part1([H1, H2|T], Count) when H1 > H2 -> part1([H2|T], Count);
part1([_|T], Count) -> part1(T, Count + 1);
part1([], Count) -> Count - 1.

%% part2(Input, 0).
part2([H1, H2, H3, H4|T], Count) when H1 + H2 + H3 < H2 + H3 + H4 -> part2([H2, H3, H4|T], Count + 1);
part2([_|T], Count) -> part2(T, Count);
part2([], Count) -> Count.

```

---

<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 1, 2021, 10:46am UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/8 "2021-12-01T10:46:47Z")

</div>

My part 1 I used lists:foldl, having failed to do it with a list comprehension. By part 2, I realised that a recursive function was the way forward and my solution was almost identical to @vkatsuba.

---

<div class="post-metadata">

**Author:** ![adolfont](https://erlangforums.com/user_avatar/erlangforums.com/adolfont/32/402_2.png) [@adolfont](https://erlangforums.com/u/adolfont)\
**Post date:** [December 1, 2021, 12:55pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/9 "2021-12-01T12:55:35Z")

</div>

My solution: [pensandoemelixir/adventofcode/2021/day01\_2021 at main · adolfont/pensandoemelixir · GitHub](https://github.com/adolfont/pensandoemelixir/tree/main/adventofcode/2021/day01_2021)

Two videos where I try to explain my code: [Advent of Code 2021 - YouTube](https://youtube.com/playlist?list=PLGj5ZUU3H99RFfw2US2WuHgkNQ5i7jGcH)

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://erlangforums.com/user_avatar/erlangforums.com/kokolegorille/32/223_2.png) [@kokolegorille](https://erlangforums.com/u/kokolegorille)\
**Post date:** [December 1, 2021, 1:32pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/10 "2021-12-01T13:32:46Z")

</div>

My solution is not typical from Erlang. It’s a translation from code I would write in Elixir.

I can see that most solutions use recursion 🙂

```erlang
-module(aoc1).
-export([part1/0, part2/0]).

part1() ->
    {_, Sum} = lists:foldl(fun(X, {Prev, Sum}) -> 
        if 
        X > Prev -> {X, Sum + 1};
        true -> {X, Sum}
        end
    end, {nil, 0}, load_data()),
    Sum.

part2() ->
    {_, Sum} = lists:foldl(fun
        (X, {{nil, B, C}, Sum}) -> 
            {{B, C, X}, Sum};
        (X, {{A, B, C}, Sum}) -> 
            if 
            B + C + X > A + B + C -> {{B, C, X}, Sum + 1};
            true -> {{B, C, X}, Sum}
            end
    end, {{nil, nil, nil}, 0}, load_data()),
    Sum.

load_data() ->
    {ok, Binary} = file:read_file("input.txt"),
    StringContent = unicode:characters_to_list(Binary),
    [element(1, string:to_integer(Substr)) || Substr <- string:tokens(StringContent, "\n")].

```

and this is the Elixir version.

```erlang
defmodule Aoc1 do
  def part1 do
    {_, sum} = load_data()
    |> Enum.reduce({nil, 0}, fn x, {prev, sum} ->
      if prev && x > prev, do: {x, sum + 1}, else: {x, sum}
    end)
    sum
  end

  def part2 do
    {_, sum} = load_data()
    |> Enum.reduce({{nil, nil, nil}, 0}, fn
      x, {{nil, b, c}, sum} ->
        {{b, c, x}, sum}
      x, {{a, b, c}, sum} ->
        if b+c+x > a+b+c,
          do: {{b, c, x}, sum + 1},
          else: {{b, c, x}, sum}
    end)
    sum
  end

  defp load_data do
    "input.txt"
    |> File.read!()
    |> String.split("\n", trim: true)
    |> Enum.map(&String.to_integer(&1))
  end
end

```

I am using AOC to try to be more fluent writing Erlang, which is the opposite direction of @bjorng 🙂

---

<div class="post-metadata">

**Author:** ![adolfont](https://erlangforums.com/user_avatar/erlangforums.com/adolfont/32/402_2.png) [@adolfont](https://erlangforums.com/u/adolfont)\
**Post date:** [December 1, 2021, 1:58pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/11 "2021-12-01T13:58:31Z")

</div>

I see most of you save some time by not trying to find a good name for the functions. As I could not find good short names, I gave them names which are too long:

- depth\_measurement\_increases/1
- depth\_measurement\_increases\_from\_sliding\_windows/1

---

<div class="post-metadata">

**Author:** ![filmor](https://erlangforums.com/user_avatar/erlangforums.com/filmor/32/988_2.png) [@filmor](https://erlangforums.com/u/filmor)\
**Post date:** [December 1, 2021, 2:25pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/12 "2021-12-01T14:25:15Z")

</div>

```erlang
solve_day1_1(List) ->
    do_solve_day1(List, lists:nthtail(1, List)).
    
solve_day1_2(List) ->
    do_solve_day1(List, lists:nthtail(3, List)).

do_solve_day1([LH|LT], [RH|RT]) when LH < RH ->
    1 + do_solve_day1(LT, RT);
do_solve_day1([_|LT], [_|RT]) ->
    do_solve_day1(LT, RT);
do_solve_day1(_, []) ->
    0.

```

The “window” is a red herring, number 1 and 2 are the same exercise with different shifts. Sadly, there is no nice way (that I know of) to make `lists:zip` behave, otherwise this could probably be golfed down quite a bit 🙂

---

<div class="post-metadata">

**Author:** ![adolfont](https://erlangforums.com/user_avatar/erlangforums.com/adolfont/32/402_2.png) [@adolfont](https://erlangforums.com/u/adolfont)\
**Post date:** [December 1, 2021, 2:28pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/13 "2021-12-01T14:28:19Z")

</div>

Yes, but you only discover that after you see Part 2. As I was doing a video, I didn’t want to change Part 1.

---

<div class="post-metadata">

**Author:** ![adolfont](https://erlangforums.com/user_avatar/erlangforums.com/adolfont/32/402_2.png) [@adolfont](https://erlangforums.com/u/adolfont)\
**Post date:** [December 1, 2021, 2:30pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/14 "2021-12-01T14:30:44Z")

</div>

There is a small (and harmless) typo in your code, @bjorng.

Line 13: inifinity  
Line 17: infinity.

---

<div class="post-metadata">

**Author:** ![MononcQc](https://erlangforums.com/user_avatar/erlangforums.com/mononcqc/32/417_2.png) [@MononcQc](https://erlangforums.com/u/MononcQc)\
**Post date:** [December 1, 2021, 2:56pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/15 "2021-12-01T14:56:13Z")

</div>

After completing the 2017 edition and rage quitting the 2019 on video, this year I’m setting a low bar for things as I’ll try to complete as much of the advent of code as I can using only Awk, because why not.

```erlang
#!/usr/bin/env bash

echo "part 1"
awk 'prev != "" && prev < $1 { incr+=1; } {prev = $1} END { print incr }' day1.input

echo "part 2"
awk \
 '# initialization steps
  a == "" { a=$0; next;}
  b == "" {b=a; a=$0; next}
  c == "" {c=b; b=a; a=$0; next}
  # calculate sliding window
  {prev=a+b+c; c=b; b=a; a=$0}
  # count if the increment is larger
  a+b+c > prev { incr+=1 }
  # end result
  END {print incr}' day1.input

```

---

<div class="post-metadata">

**Author:** ![bunny](https://erlangforums.com/user_avatar/erlangforums.com/bunny/32/346_2.png) [@bunny](https://erlangforums.com/u/bunny)\
**Post date:** [December 1, 2021, 6:05pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/16 "2021-12-01T18:05:00Z")

</div>

Awk indeed … I like your approach! Having read a couple of Haskell books but never having written any actual code, I’m using AoC as an excuse to practice. It’s good to remember that it’s okay to suck at something.

---

<div class="post-metadata">

**Author:** ![Rainer](https://erlangforums.com/user_avatar/erlangforums.com/rainer/32/210_2.png) [@Rainer](https://erlangforums.com/u/Rainer)\
**Post date:** [December 1, 2021, 7:42pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/17 "2021-12-01T19:42:05Z")

</div>

Nice to see many erlang solutions this year 😃  
Unfortunately I’ve got absolutely no time to participate this year ☹

---

<div class="post-metadata">

**Author:** ![vkatsuba](https://erlangforums.com/user_avatar/erlangforums.com/vkatsuba/32/314_2.png) [@vkatsuba](https://erlangforums.com/u/vkatsuba)\
**Post date:** [December 1, 2021, 8:27pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/18 "2021-12-01T20:27:35Z")

</div>

> [@Rainer](#):
>
> Unfortunately I’ve got absolutely no time to participate this year

Sounds sad. Because this is a great opportunity to practice with Erlang developers together. Some, pair programming of challenges 🙃. Do not miss the opportunity to practice with Erlang - this is a great way to learn something new and develop Erlang skills, I think that those challenges are useful not only for newcomers but also for experienced Erlang developers 🤘. But of course, up to you 🙃.

---

<div class="post-metadata">

**Author:** ![Rainer](https://erlangforums.com/user_avatar/erlangforums.com/rainer/32/210_2.png) [@Rainer](https://erlangforums.com/u/Rainer)\
**Post date:** [December 1, 2021, 8:48pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/19 "2021-12-01T20:48:31Z")

</div>

Yes, that’s why I did it last year already with Erlang 🙂 I’d do it again this year, if I had time…  
But the reason I simply have no time this year is not sad: my second daughter was born yesterday 😀  
So I wish you all a lot of fun, while I’ll be enjoying some sleepless nights haha

---

<div class="post-metadata">

**Author:** ![vkatsuba](https://erlangforums.com/user_avatar/erlangforums.com/vkatsuba/32/314_2.png) [@vkatsuba](https://erlangforums.com/u/vkatsuba)\
**Post date:** [December 1, 2021, 8:54pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/20 "2021-12-01T20:54:41Z")

</div>

> [@Rainer](#):
>
> my second daughter was born yesterday

This is amazing news! Congratulations!

---

<div class="post-metadata">

**Author:** ![AstonJ](https://erlangforums.com/user_avatar/erlangforums.com/astonj/32/9_2.png) [@AstonJ](https://erlangforums.com/u/AstonJ)\
**Post date:** [December 1, 2021, 10:16pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/21 "2021-12-01T22:16:38Z")

</div>

> [@Rainer](#):
>
> my second daughter was born yesterday 😀

Congrats Rainer 🥳 👶 🍼 :041:

> [@Rainer](#):
>
> while I’ll be enjoying some sleepless nights haha

Maybe you could combine the two :003:

---

<div class="post-metadata">

**Author:** ![adolfont](https://erlangforums.com/user_avatar/erlangforums.com/adolfont/32/402_2.png) [@adolfont](https://erlangforums.com/u/adolfont)\
**Post date:** [December 1, 2021, 10:30pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-1/710/22 "2021-12-01T22:30:35Z")

</div>

Congrats!

[Next page](https://erlangforums.com/t/advent-of-code-2021-day-1/710.md?page=2)
