Aetherus
Advent of Code 2020 - Day 1
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)
Most Liked
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.
list =
File.read!("input")
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.to_integer/1)
[{a, b} | _] = for i <- list, j <- list, i + j == 2020, do: {i, j}
IO.puts("Part1: #{a} x #{b} = #{a * b}")
[{a, b, c} | _] = for i <- list, j <- list, k <- list, i + j + k == 2020, do: {i, j, k}
IO.puts("Part2: #{a} x #{b} x #{c} = #{a * b * c}")
I’m putting my solutions on Github.
8
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.
4
mexicat
My solution:
defmodule AdventOfCode.Day01 do
def part1(input) do
{a, b} =
input
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.to_integer/1)
|> find_two_entries_that_sum_to_2020()
a * b
end
def part2(input) do
{a, b, c} =
input
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.to_integer/1)
|> find_three_entries_that_sum_to_2020()
a * b * c
end
def find_two_entries_that_sum_to_2020(entries) do
Enum.reduce_while(entries, nil, fn entry, _acc ->
n_to_find = 2020 - entry
case Enum.find(entries, fn x -> x == n_to_find end) do
nil -> {:cont, nil}
x -> {:halt, {entry, x}}
end
end)
end
def find_three_entries_that_sum_to_2020(entries) do
try do
for a <- entries,
b <- entries,
c <- entries,
a + b + c == 2020,
# exit as soon as we find the solution
do: throw({:break, {a, b, c}})
catch
{:break, result} -> result
end
end
end
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.
3
Popular in Challenges
This topic is about Day 3 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 17 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
Note: This topic is to talk about Day 10 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can jo...
New
This topic is about Day 15 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Here is my solution for day 1 of Advent of Code:
defmodule Day01 do
def part1(input) do
all = parse(input)
{first, second} = E...
New
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New
New
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New
Note: This topic is to talk about Day 9 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








