# Advent of Code 2021 - Day 6

**URL:** <https://erlangforums.com/t/advent-of-code-2021-day-6/745>\
**Category:** Coding Challenges\
**Tags:** advent-of-code\
**Created:** [December 6, 2021, 4:59am UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745 "2021-12-06T04:59:08Z")\
**Posts on this page:** 8\
**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 6, 2021, 4:59am UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/1 "2021-12-06T04:59:08Z")

</div>

This topic is about Day 6 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:** ![Gwaeron](https://erlangforums.com/user_avatar/erlangforums.com/gwaeron/32/833_2.png) [@Gwaeron](https://erlangforums.com/u/Gwaeron)\
**Post date:** [December 6, 2021, 5:37am UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/2 "2021-12-06T05:37:47Z")

</div>

Pretty easy one today. I used the solution they were obviously trying to trick you into using by representing the fish state as a list for part 1 and appending new fish as they spawn, before changing it to a map of counters to be able to run part 2.

Edit: Here’s a solution - did it in Go at first, which is famously verbose, and I wanted to see how much cleaner it would look in Erlang. Quite a bit, and I didn’t spend much time trying to make it pretty.

```erlang
-module(day06).
-export([main/0]).

main() ->
    Init = parse("input/day06.txt"),
    io:format("Part 1: ~p~nPart 2: ~p~n", [run(Init, 80), run(Init, 256)]).

run(Fish, 0) -> lists:sum(maps:values(Fish));
run(Fish, Days) -> run(tick(Fish), Days - 1).

tick(Fish) ->
    Spawning = maps:get(0, Fish, 0),
    Decremented = lists:foldl(fun shift_down/2, Fish, lists:seq(1, 8)),
    Decremented#{8 => Spawning,
                 6 => maps:get(6, Decremented, 0) + Spawning}.

shift_down(Ix, Map) ->
    Map#{Ix - 1 => maps:get(Ix, Map, 0)}.

parse(File) ->
    {ok, BinData} = file:read_file(File),
    Str = binary_to_list(BinData),
    Trimmed = string:trim(Str),
    freq([list_to_integer(Token) || Token <- string:tokens(Trimmed, ",")]).

freq(Fish) ->
    lists:foldl(fun increment_at/2, #{}, Fish).

increment_at(Ix, Map) ->
    maps:update_with(Ix, fun(N) -> N + 1 end, 1, Map).

```

---

<div class="post-metadata">

**Author:** ![danilagamma](https://erlangforums.com/user_avatar/erlangforums.com/danilagamma/32/342_2.png) [@danilagamma](https://erlangforums.com/u/danilagamma)\
**Post date:** [December 6, 2021, 6:03am UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/3 "2021-12-06T06:03:37Z")

</div>

@Gwaeron that’s how it went for me 😅 :

```erlang
main(File) ->
    {ok, RawData} = file:read_file(File),
    Data = [binary_to_integer(X) || X <- binary:split(RawData, <<",">>, [global, trim]) ],
    io:format("part 1: ~p~n", [solve1(Data)]),
    io:format("part 2: ~p~n", [solve2(Data)]),
    ok.

solve1(Data) ->
    length(simulate_n(Data, 80)).

simulate_n(Data, Days) ->
    lists:foldl(fun (_, Acc) -> [NewFishes || F <- Acc, NewFishes <- simulate(F)] end,
                Data,
                lists:seq(1, Days)).

simulate(0) -> [6, 8];
simulate(X) -> [X - 1].

solve2(Data) ->
    OneToFive = maps:values(freq(Data)),
    Initial = list_to_tuple([0] ++ OneToFive ++ [0, 0, 0]),
    lists:sum(tuple_to_list(simulate_count_n(Initial, 256))).

simulate_count_n(Acc, 0) -> Acc;
simulate_count_n(Acc, N) ->
    simulate_count_n(simulate_count(Acc), N - 1).

simulate_count({V0, V1, V2, V3, V4, V5, V6, V7, V8}) ->
    {V1, V2, V3, V4, V5, V6, V7 + V0, V8, V0}.

freq(List) ->
    lists:foldl(fun (K, Acc) -> maps:update_with(K, fun (V) -> V + 1 end, 1, Acc) end,
                #{},
                List).

```

---

<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 6, 2021, 1:40pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/4 "2021-12-06T13:40:33Z")

</div>

> [@Gwaeron](#):
>
> I used the solution they were obviously trying to trick you into using by representing the fish state as a list for part 1 and appending new fish as they spawn, before changing it to a map of counters to be able to run part 2.

Ditto.

Buy the way, there’s an [Unofficial AoC 2021 Survey](https://forms.gle/pucYXedo1JYmWe8PA) - would be good if more than one person ticked Erlang 🙂

---

<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 6, 2021, 1:45pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/5 "2021-12-06T13:45:00Z")

</div>

Done in Awk first as I respect my initial engagement, but felt it would have been so much easier in Erlang:

```erlang
BEGIN { FS="," }
{ for (i=1; i<=NF; i++) { l[$i]++ } }
END {
    for (i=1; i<=DAYS; i++) {
        t=l[0]
        for (j=0; j<8; j++) { l[j]=l[j+1] }
        l[8]=t; l[6]+=t
    }
    for (i in l) { c+=l[i] }
    print c
}

```

For comparison:

```erlang
main([File]) ->
    {ok, Bin} = file:read_file(File),
    T = lists:foldl(
          fun(N,T) -> setelement(N+1, T, element(N+1,T)+1) end,
          {0,0,0,0,0,0,0,0,0},
          [binary_to_integer(B) || L <- string:split(Bin, "\n", all), L =/= <<>>,
                                   B <- string:split(L, ",", all)]
    ),
    io:format("~p~n", [run(256, T)]).

run(0,{A,B,C,D,E,F,G,H,I}) -> A+B+C+D+E+F+G+H+I;
run(N,{A,B,C,D,E,F,G,H,I}) -> run(N-1, {B,C,D,E,F,G,H+A,I,A}).

```

---

<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 6, 2021, 8:11pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/6 "2021-12-06T20:11:22Z")

</div>

Part 1:

```erlang
part1() ->
  Bin = <<"3,4,3,1,2\n">>,
  Data = [binary_to_integer(X) || X <- re:split(Bin, "[,/\n]+"), X /= <<>>],
  length(simulation(256, Data))

simulation(0, Fishs) -> Fishs;
simulation(Days, Fishs) -> simulation(Days - 1, lanternfish(Fishs)).

lanternfish([]) -> [];
lanternfish([0|T]) -> [6 | [8 | lanternfish(T)]];
lanternfish([H|T]) -> [H - 1 | lanternfish(T)].

```

And… After crashing of my shell 😜:  
Part 2:

```erlang
part2() ->
  {ok, Bin} = file:read_file("input"),
  Data = [binary_to_integer(X) || X <- re:split(Bin, "[,/\n]+"), X /= <<>>],
  Acc = #{0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0},
  gen(256, init(Data, Acc)).

init([], Acc) ->
  Acc;
init([H|T], Acc) ->
  init(T, Acc#{H => maps:get(H, Acc) + 1}).

gen(0, #{0 := D0, 1 := D1, 2 := D2, 3 := D3, 4 := D4, 5 := D5, 6 := D6, 7 := D7, 8 := D8}) ->
  D0 + D1 + D2 + D3 + D4 + D5 + D6 + D7 + D8;
gen(Days, #{0 := D0, 1 := D1, 2 := D2, 3 := D3, 4 := D4, 5 := D5, 6 := D6, 7 := D7, 8 := D8}) ->
  gen(Days - 1, #{0 => D1, 1 => D2, 2 => D3, 3 => D4, 4 => D5, 5 => D6, 6 => D0 + D7, 7 => D8, 8 => D0}).

```

🤘

---

<div class="post-metadata">

**Author:** ![michallepicki](https://erlangforums.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@michallepicki](https://erlangforums.com/u/michallepicki)\
**Post date:** [December 6, 2021, 9:12pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/7 "2021-12-06T21:12:48Z")

</div>

Day 6 in Sesterl - [adventofcode/main.sest at master · michallepicki/adventofcode · GitHub](https://github.com/michallepicki/adventofcode/blob/master/2021/sesterl/6/src/main.sest)

---

<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 6, 2021, 10:02pm UTC](https://erlangforums.com/t/advent-of-code-2021-day-6/745/8 "2021-12-06T22:02:44Z")

</div>

Thanks. @Gwaeron. Your first few words helped me solve today’s puzzle: [pensandoemelixir/day06\_v2.erl at main · adolfont/pensandoemelixir · GitHub](https://github.com/adolfont/pensandoemelixir/blob/main/adventofcode/2021/day06_2021/day06_v2.erl)

Ugly as usual, but I believe I am learning.
