Advent of Code - Day 1

Note by the Moderators: This topic is to talk about the first day of the Advent of Code.

For general discussion about the Advent of Code 2018 and links to topics of the other days, see this topic.


Nice one, mine came out quite similar: solution & test

4 Likes

Ah nice! Great minds I guess :brain:

2 Likes

I’m doing this too and my solution is very similar to other ones already posted.

I’m fairly new to Elixir and only using it as and when I can for a side-project so I learnt a few new things…

  1. Stream.cycle() was new to me and I had to Google to find it.

  2. Enum.reduce_while was also a new discovery but at least I found this one by looking through the documentation.

  3. List is really slow and MapSet is really fast when checking for existing items. My first solution used List and was fine for the tests I built but I thought that I had a bug and an infinite loop when I ran the full set of data for the challenge. Once it eventually finished I thought that there must be a faster way and looked at the documentation.

Overall it was a fun little challenge and exactly what I hoped it would be… something to get me to learn new things, not just a test to see if I know it all.

I’m really looking forward to watching José’s Twitch streams about these challenges too.

8 Likes

I solved the first one using Enum.reduce:

def part_one(input) do
  input
  |> String.split("\n", trim: true)
  |> Enum.reduce(0, &(String.to_integer(&1) + &2))
end

Saves some keystrokes, but yeah, the code looks horrible compared to the Enum.map version. Would love to see different implementations for the second one.

I decided to partake as well. Surprise surprise. I ended up with a very similar solution.

1 Like

Mine feels a little hacky and relies on Elixir/Erlang being better at tracking unique anything than me. Part 2 of day 1 started off at like ~15s for 137000+ times through; I got it down to 1.5s today a bit ago. https://github.com/aleph-naught2tog/aoc_18/blob/master/day_1/main.ex

1 Like

I just finished my solutions for Day 1. They are here:

The code turned out pretty clean, so I’m happy :slight_smile:

(I spent half the day trying to use Stream.transform, and could not get it to work — I thought it would work like Enum.reduce, but it doesn’t. I was able to get it working with Enum.reduce_while finally. Yay!

The one thing that I learned the hard way is that Stream’s can be piped to Enum functions (like find / reduce / etc) well — I thought any Enum function after a Stream pipeline would try to fetch the entire stream, but I was wrong)

1 Like

Just finished Day 1 both parts, the fun thing with Advent of Code is that basically anything that deals with reading an input file and doing some calculation on it is a prime candidate for File.stream and then Stream processing–we have it really easy in the Elixir world.

4 Likes

Haha, my Day1 Part2, started off at ~171s (¬¬) but got it down to ~0.16s (165ms) :smiley:

I’ve been working in elixir for a year now and most of the language “constructs” I used in this day’'s solutions including the ones you mentioned were all new to me. My implementation for the second problem used lists and was very slow. I tried it with mapsets after looking at others solutions and wow what a difference.

After this first day, I’m also looking forward to the next days and to Jose’s twitch streams.

3 Likes

Total Elixir n00b here, So glad my solution is quite close to this! Learnt a few tricks as well thank you