bjorng
Erlang Core Team
Advent of Code 2019 - Day 2
Note: This topic is to talk about Day 2 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can join it by following this link and entering the following code:
39276-eeb74f9a
Most Liked
ferd
Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger
My solution, in Erlang:
7
sasajuric
Author of Elixir In Action
It’s not all that difficult to also generate permutations lazily:
0..99
|> Stream.flat_map(fn noun -> Stream.map(0..99, &{noun, &1}) end)
My first version used this approach, but then I decided to switch to for b/c I think it communicates the intention a bit better.
4
michallepicki
I’m continuing to write really ugly Gleam code - but it works so far 
4
bjorng
Erlang Core Team
Here is my solution.
3
hauleth
My solution:
defmodule GravAssist do
def calc([op, _, _ | rest], a \\ 12, b \\ 2) do
hd(execute([op, a, b | rest]))
end
def execute(bytecode) when is_list(bytecode), do: execute(:array.from_list(bytecode))
def execute(bytecode), do: eval(bytecode, 0)
defp eval(bytecode, ic) do
case :array.get(ic, bytecode) do
99 -> :array.to_list(bytecode)
op when op in 1..2 ->
pos_a = :array.get(ic + 1, bytecode)
pos_b = :array.get(ic + 2, bytecode)
pos_r = :array.get(ic + 3, bytecode)
result = compute(op, :array.get(pos_a, bytecode), :array.get(pos_b, bytecode))
new_bc = :array.set(pos_r, result, bytecode)
eval(new_bc, ic + 4)
_ -> :error
end
end
defp compute(1, a, b), do: a + b
defp compute(2, a, b), do: a * b
end
data =
IO.read(:line)
|> String.split(",")
|> Enum.map(&String.to_integer(String.trim(&1)))
IO.inspect(GravAssist.calc(data), label: :sol1)
for(a <- 0..99,
b <- 0..99,
19690720 == GravAssist.calc(data, a, b),
do: {a, b})
|> IO.inspect(label: :sol2)
3
Popular in Challenges
Note by the Moderators: This topic is to talk about the Day 2 of the Advent of Code.
For general discussion about the Advent of Code 201...
New
This topic is about Day 18 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
New
Note: This topic is to talk about Day 12 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
The second part of today’s puzzle is very misleading.
FYI, each of the ghosts has only one possible position that ends with a "Z" on its...
New
This topic is about Day 15 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
This topic is about Day 2 of the Advent of Code 2020.
New
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
Note: This topic is to talk about Day 4 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Hi, there :wave:
Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New







