Anyone planning on solving Advent of Code 2020?

I always start with excitement and then get busy on 9/10th day. This year, like the year before this, and the year before that, I intend to be regular. My language of choice this year would be Elixir (and/or Dart depending on my mood on the day). Anyone else going to solve Advent of Code puzzles starting tomorrow?

Waiting for the first one to unlock :wink: https://adventofcode.com/

5 Likes

Share link for this tweet.

I remember when that first came out ā€“ I was still living on the east coast and ended up staying up past midnight every night for three weeks straight. Now I wonā€™t touch it, even though Iā€™m on the west coast and the puzzles come out at 9:00 ā€“ Iā€™m too competitive to ignore the leaderboard and I just canā€™t risk my sleep!

Be sure to brush up on your regex!

2 Likes

Is it much more difficult if attempting in a ā€œmanual memoryā€ kind of language like Rust?

2 Likes

I have a friend who solves these things blazingly fast with C and C++, so Iā€™m not sure if itā€™s that much more difficult, I guess it all depends on how much practice one has in a language?

There were some problems I found a little hard to solve in Elixir, sometimes I got into reduce-hell (my fault), and once I just used :digraph

@rgd43 if you do attempt to solve it with a ā€œmanual memoryā€ language do let us know of your experience :slight_smile:

@gianthamster that explains why I am suddenly so proficient with regex during the end of the year :smiley:

2 Likes

I would be lost without sites like rubular.com :see_no_evil: :joy:

Do it RDTā€™er! :nerd_face:

Let us know how you get on @mafinar :+1:

1 Like

I thought to participate this year(btw it will be my first time), and I will use elixir as my main language on the advent of code! o/

3 Likes

Hereā€™s the first one:

3 Likes

thanks @AstonJ - I never heard of rubular.com

I use regex101

3 Likes

Thatā€™s a very pretty use of Enum.reduce_while, and I love how youā€™ve set up the entire project. Iā€™m always hacking this sort of thing out in one-off scripts.

Going to keep following this, I think ā€“ it looks like Iā€™m going to learn some things.

3 Likes

Thanks. I tend to be very lazy and at the same time am very picky about code consistency. So figured, a little generator wonā€™t hurt. Yeah maybe Iā€™ll keep posting my hiccups and solutions here, most of the time I did Advent of Code solo and ended up bored after a week or so, this time probably discussions and help from yā€™all would help me learn things and stay motivated :slight_smile:

2 Likes

This is todayā€™s (Day 2) one. Regex started to appear.

2 Likes

As Iā€™m trying to learn Erlang, I did the first one, but I donā€™t think Iā€™ll continue as it takes too much time :wink:

-module(day1).
-export([run/0]).

run()->
    process_list(load_file("day1input.txt")).

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

process_list([H | T])->
    case process_sublist(H, T) of
        notfound -> process_list(T);
        {found, X, Y, Result} -> {X, Y, Result}
    end;
process_list([])->
    notfound.

process_sublist(Elem, [H | T]) ->
    case is_solution(Elem, H) of
        true -> {found, Elem, H, H*Elem};
        false -> process_sublist(Elem, T)
    end;
process_sublist(_, []) ->
    notfound.

is_solution(X, Y) ->
    X + Y =:= 2020.
1 Like

Me too. The book has just arrived, but you already know that :wink: ā€¦ I think I will pass on AoC with Erlang this year. This is the first time I intend to learn a language slowly and know the theories before getting my hands dirty :slight_smile:

1 Like

Solution of day 3 in Elixir.

Suggestion Needed: I have a gut feeling I could make the code beautiful with pattern matching and avoiding the map to matrix transformation. If any of you did it that way or like to give me ideas on it thatā€™d be appreciated <3

1 Like

After my first version of Day1 I wantet to try another approach with much less code, it became a one-liner :wink:

-module(day1).
-export([run/0]).

run()->
    process_list(load_file("day1input.txt")).

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

process_list(L)->
    [{X, Y, X*Y} || X <- L, Y <- L, X + Y =:= 2020 ].
3 Likes

I miss Erlang/Python style list comprehension in Elixir.

3 Likes

Thatā€™s making me want to get back in to reading the book!

Soon I hope :nerd_face: :orange_book:

1 Like

Despite having no time, I did day 2, as itā€™s quite addictive :smiley:
Biggest problem was that i had no idea how to parse a string in erlang :man_shrugging:
But now Iā€™m quite satisfied with my solution :slight_smile:

-module(day2).
-export([part1/0, part2/0]).

part1()->
    ParsedLines = load_file("day2input.txt"),
    ValidLines = [ Line || Line <- ParsedLines, is_valid1(Line)],
    length(ValidLines).

part2()->
    ParsedLines = load_file("day2input.txt"),
    ValidLines = [ Line || Line <- ParsedLines, is_valid2(Line)],
    length(ValidLines).

is_valid1({From, To, Char, Pw}) ->
    N = length([[X] || X <- Pw, [X] =:= Char]),
    (N >= From) and (N =< To).

is_valid2({Idx1, Idx2, Char, Pw}) ->
    (string:slice(Pw, Idx1-1, 1) =:= Char) xor (string:slice(Pw, Idx2-1, 1) =:= Char).

load_file(Filename)->
    {ok, Binary} = file:read_file(Filename),
    StringContent = unicode:characters_to_list(Binary),
    [ parse(Line) || Line <- string:tokens(StringContent, "\n")]. 

parse(Line)->
    {ok, MP} = re:compile("([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)"),
    {_,[_,From, To, Char, Pw]} = re:run(Line, MP),
    {extract_num(Line, From), extract_num(Line, To), extract_str(Line, Char), extract_str(Line, Pw)}.

extract_str(String, {Start, End})->
    string:slice(String, Start, End).

extract_num(String, {Start, End})->
    Str = string:slice(String, Start, End),
    {Int, _} = string:to_integer(Str),
    Int.
1 Like

Todayā€™s one was boring :confused:

1 Like