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.
Iām possibly going to have to refactor my repository structure but in any event my solution is here.
The 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 thepriv/
project directory. You know, convenience. - Have a
fuel
andtotal_fuel
functions, 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.
Brofist?
My solution was pretty vanilla.
If the boilerplate strategy above holds, I might have to sneak something like that in!