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

JEG2
Note: This topic is to talk about Day 9 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics ...
New
sneako
Note by the Moderators: This topic is to talk about the first day of the Advent of Code. For general discussion about the Advent of Code...
New
Aetherus
This topic is about Day 5 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bjorng
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 joi...
New
bjorng
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
cblavier
Hey there :wave: No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
bjorng
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
Qqwy
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
igorb
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
stefanchrobot
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement