bjorng

bjorng

Erlang Core Team

Advent of Code 2020 - Day 18

This topic is about Day 18 of the Advent of Code 2020 .

Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/leaderboard/private/view/39276

The join code is:
39276-eeb74f9a

Most Liked

akash-akya

akash-akya

Hacked with elixir quoted expression, this can be called cheating I guess :slight_smile:

Since the input is a valid elixir expression, getting AST is just treating input as elixir code, we dont even have to care about the brackets.

defmodule Advent2020.Day18 do
 def input do
   File.read!(Path.expand("day18.txt", :code.priv_dir(:advent_2020)))
   |> String.split("\n", trim: true)
 end

 defp replace(str, []), do: str

 defp replace(str, [{match, replacement} | rest]),
   do: String.replace(str, match, replacement) |> replace(rest)

 def parse_with_replcement(line, replace) do
   {:ok, quoted} = replace(line, replace) |> Code.string_to_quoted()
   quoted
 end

 defp replace_operation(num, _replacement) when is_integer(num), do: num

 defp replace_operation({operator, metadata, [a, b]}, replace) do
   {replace[operator] || operator, metadata,
    [replace_operation(a, replace), replace_operation(b, replace)]}
 end

 def run(replace) do
   revert = Enum.map(replace, fn {m, r} -> {String.to_atom(r), String.to_atom(m)} end)

   Enum.map(input(), fn line ->
     {result, []} =
       parse_with_replcement(line, replace)
       |> replace_operation(revert)
       |> Code.eval_quoted()

     result
   end)
   |> Enum.sum()
 end

 def part_one, do: run([{"*", "-"}])
 def part_two, do: run([{"*", "-"}, {"+", "/"}])
end

camilleryr

camilleryr

I originally parsed each equation into an expression tree to evaluate them - which worked, but then I saw on reddit someone mention the shunting yard algorithm - did a bunch of reading on operator precedence parsing and stack based calculators and was able to refactor my code in a really satisfying way. The implementation of the algorithm and the evaluator could use some clean up, but it works, and it works about 10 times faster then the expression tree approach

https://github.com/camilleryr/advent20/blob/main/lib/day_18.ex

bjorng

bjorng

Erlang Core Team

I spent most of the time in part 1 trying to get the recursive descent parser to make the evaluating order left-to-right. When I solved that part 2 was easy.

Here is my solution.

Where Next?

Popular in Challenges Top

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
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
Aetherus
Hello, guys. I’m back again, but only for the weekends, maybe. This topic is about Day 13 of the Advent of Code 2020 . Thanks to @egze,...
New
LostKobrakai
This one has been quite the ride. Struggled at first to find a good data format to suite the problem. I really like how that turned out b...
New
stevensonmt
Reasonably pleased with my solution. The bitstring packet problems are so well suited to Erlang/Elixir it’s almost not fair. defmodule D...
New
sb8244
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
coen.bakker
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
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
Aetherus
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby. Anyway, here is my solution: ...
New
lud
Hello everyone! This year is going to be shorter, but the difficulty will grow faster. Today I already feel that this is not standard “D...
New

Other popular topics Top

New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement