Note: This topic is to talk about Day 1 of the Advent of Code 2019.
I used Advent of Code last year to learn some Elixir. Since I haven’t used Elixir much this year, I am using AOC 2019 as an opportunity to refresh my Elixir skills.
Here is my solution for my solution for Day 1.
There is room for improvement, eg by using
Enum instead of
Stream, and probably even more by doing manual recursion, but as I’m in the area of 0.05ms for part 2, I’ll just leave it as is.
That‘s a cool Boilerplate you got there. Gonna borrow it next time
I will try something new this year, will live stream solving the challenges on Twitch. Today the stream starts at 2pm CET (in 1 hour 15 mins), here’s my channel Will be using Elixir, and maybe Rust for some challenges.
Last year’s solutions are here.
P.S. First day solution is here. Forgot to switch on “save past broadcasts”, will not forget next time Next stream will be on Tuesday at 7pm CET.
Here’s my take.
here’s mine github repo for this aoc2019: alvises/aoc2019
I’m possibly going to have to refactor my repository structure but in any event my solution is here.
aoc package is going to hold common code such as functions to load data from files.
Each day will be its own application and, compared to most I imagine, verbose. I like adding tests and some documentation.
I am so pleased that Saša is doing this again this year. His code tends to be incredibly compact and contains some lovely approaches which I’d not have thought about. Today I’ll be learning more about streams from him.
I did mine in iex and translated to code after the fact: https://github.com/keathley/advent_of_code_2019/blob/master/day1.exs.
My own stab at Day 1:
This year I’m going to do my best to take a stab at this, using Elixir of course! Here’s my Day 1 solutions. I’m aiming for idiomatic elixir, that’s not too code-golfy.
Finished my pretty naive implementation as well: https://github.com/dimitarvp/aoc2019.ex/blob/master/lib/day1.ex
I opted for:
- Common module helpers, for now just
File.stream!-ing standardised file names from the
priv/project directory. You know, convenience.
- Have a
total_fuelfunctions, the latter being the recursive one.
- Introducing doctests that exactly match the examples given in the task description.
Again, pretty naive and straightforward. Comments welcome!
@sasajuric Almost like mine! I also opted for a helper module that streams the file that maps to the day in question.
def iterate(mass) do mass |> Stream.iterate(&fuel_required/1) |> Enum.take_while(& &1 > 0) |> tl() # remove the module mass which would be the first in the list |> Enum.sum() end
That’s really clever, I love it!
@NobbZ Really solid code.
Yeah, I have a few common helpers, but I try to keep this as small as I can. So mostly reading the file input, and a few frequently needed enum helpers.
Doing the challenge in Elixir this year. My day one solution is a little ugly but gets the job done. https://github.com/agundy/advent_of_code/blob/master/2019/solutions/lib/day_1.ex
Nice. One of my top priorities are terseness and readability. But I can give up some terseness in exchange for code that’s quicker to parse for any random reading human, and it seems you are the same.
My solution was pretty vanilla.
If the boilerplate strategy above holds, I might have to sneak something like that in!