Aetherus
Finished Day 1 with Elixir ![]()
Here’s my code:
#!/usr/bin/env elixir
defmodule Combination do
@doc "Yields each combination of 2"
def c2(list, _fun) when length(list) < 2, do: :ok
def c2([a|tail], fun) do
Enum.each(tail, fn(b)-> fun.(a, b) end)
c2(tail, fun)
end
@doc "Yields each combination of 3"
def c3(list, _fun) when length(list) < 3, do: :ok
def c3([a|tail], fun) do
c2(tail, fn(b, c)-> fun.(a, b, c) end)
c3(tail, fun)
end
end
nums = "./day1.txt"
|> File.stream!([], :line)
|> Stream.map(&String.trim/1)
|> Enum.map(&String.to_integer/1)
Combination.c2(nums, fn(a, b)->
if a + b == 2020 do
IO.inspect(a * b, label: "Part 1")
end
end)
Combination.c3(nums, fn(a, b, c)->
if a + b + c == 2020 do
IO.inspect(a * b * c, label: "Part 2")
end
end)
Trending in Challenges
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Aetherus
Refactored a bit of my code:
adamu
Here’s mine for Day 1. It’s my second implementation, after I realised during part 2 that comprehensions made the whole thing much simpler.
I’m putting my solutions on Github.
LostKobrakai
Part 1 can be optimized by spliting the list in half between greater 1010 and smaller and only searching for a pairs of one number in each. There cannot be two numbers smaller or two numbers greater than 1010 to add up to 2020.
Edit: I’ve added some benchmarks.
Hanspagh
I did the exact same thing
cblavier
So did I!
My first version was more refined : it was preventing the same combinations to be generated and stopped the function at the first match.
But same performance for both solutions
LostKobrakai
I found improved performance by preventing iterating the list for cases, which are already beyond the allowed sum (unoptimized is 23x slower): aoc2020/lib/aoc2020/day1.ex at master · LostKobrakai/aoc2020 · GitHub
mexicat
My solution:
After finishing part 1 I realized that my approach would not work with part 2, so they’re quite different
I tried to make both parts efficient, so they stop calculation as soon as they find a correct result.
adamu
You probably shouldn’t be including the file reading/parsing in the calculations - I bet that’s throwing everything off.
Hanspagh
Is there no other way to short circuit comprehensions than
tryandthrow?LostKobrakai
Seems like it, but that made it even more different
Details