# Advent of Code 2022 - Day 1 Discussions

This topic is about Day 1 of the Advent of Code 2022.

With less than 2 hours to go for Advent of Code 2022, I am leaving this post open so that folks can discuss their `day 1` adventures when it begins (12:00 AM EST)!

The entry code is:
`370884-a6a71927`

Good luck!

3 Likes

My solution:

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

solve1(Data) ->
lists:max([ lists:sum(X) || X <- Data ]).

solve2(Data) ->
lists:sum(lists:sublist(lists:reverse(lists:sort([ lists:sum(X) || X <- Data ])), 3)).
``````
3 Likes

I used `:gb_sets` to get top 3. It doesn’t help much since it is `O(n.lg(n))` vs `O(lg(n))` and `n` here is not large. But felt like let’s just try this (Elixir though)

1 Like

Hi the links are still 2021 you should change them to 2022

2 Likes

Thank you!

1 Like

I also used a series of split, map, sum and max from the lists module.
As I did not read the requirements to the end, I also implemented the calculation of which elf carried the most calories (= index of max in the list of sums).
It seems that the lists module does not provide such a function

``````find(Elem :: term(), List :: list()) -> Index :: integer() | enoent.

find(Elem, List) ->
find(Elem, List, 1).
find(Elem, [], Idx) ->
enoent;
find(Elem, [Elem|_List], Idx) ->
Idx;
find(Elem, [_|List], Idx) ->
find(Elem, List, Idx + 1).
``````

or did I not look close enough?

2 Likes

Yes, looks like there is not `Enum.find_index` equivalent in `lists` module. However, can anyone explain why `string:str(List, [Elem]) - 1` yields the desired index?

1 Like

I started doing Erlang solution. I am not used to Erlang so might look funny my code. Here’s Day 1:

``````-module(day_1).

-export([solve/0]).

input() ->
RawCalories.

solve() ->
RawCalories = input(),
ParsedData = parse(RawCalories),
Calories = [lists:sum(X) || X <- ParsedData],
Solve1 = solve1(Calories),
Solve2 = solve2(Calories),

{Solve1, Solve2}.

solve1(Calories) -> lists:max(Calories).

solve2(Calories) ->
SortedCalories =
lists:reverse(
lists:sort(Calories)),
Top3 = lists:sublist(SortedCalories, 3),
lists:sum(Top3).

parse(RawCalories) ->
[[binary_to_integer(Calory) || Calory <- ElfCalories]
|| ElfCalories
<- [binary:split(ElfCalories, <<"\n">>, [global, trim])
|| ElfCalories
<- [Calories
|| Calories <- binary:split(RawCalories, <<"\n\n">>, [global, trim])]]].

``````
2 Likes