bjorng

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

ferd

Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger

My solution, in Erlang:

sasajuric

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.

michallepicki

michallepicki

I’m continuing to write really ugly Gleam code - but it works so far :man_shrugging:

bjorng

bjorng

Erlang Core Team

Here is my solution.

hauleth

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)

Where Next?

Popular in Challenges Top

NobbZ
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
bjorng
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
bjorng
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
Aetherus
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
bjorng
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
New
adamu
Probably not the most efficient implementation, because part 1 took &gt;1 ms and part 2 &gt;4ms, but the code was simple enough. def p...
New
bjorng
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
cblavier
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 Top

shahryarjb
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
JorisKok
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
albydarned
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
itssasanka
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
axelson
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...
239 45766 226
New
AstonJ
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
ycv005
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New

We're in Beta

About us Mission Statement