This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the proper BEAM test for https://github.com/uber/denial-by-dns. The Erlang test in that repo is sequential | |
# (https://github.com/uber/denial-by-dns/blob/b809cc561a691d9d6201d06d38d06c33c9c9f9ec/erlang-httpc/main.erl) | |
# which is not consistent with the test description (https://github.com/uber/denial-by-dns/tree/b809cc561a691d9d6201d06d38d06c33c9c9f9ec#how-it-works) | |
# and also differs from e.g. go test in the same repo which issues requests concurrently. | |
# | |
# Consequently, the conclusion in that repo as well as the original article (https://eng.uber.com/denial-by-dns/) is incorrect. | |
# A properly written Erlang test would pass, as demonstrated by this Elixir script. | |
# | |
# This code performs a slightly refined and a correct version of that tests in Elixir: | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SqlParser do | |
def run() do | |
input = "select col1 from ( | |
select col2, col3 from ( | |
select col4, col5, col6 from some_table | |
) | |
) | |
" | |
IO.puts("input: #{inspect(input)}\n") | |
IO.inspect(parse(input)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day25 do | |
def part1(), do: | |
input() | |
|> run_machine() | |
|> tape() | |
|> Stream.filter(&match?({_pos, 1}, &1)) | |
|> Enum.count() | |
defp run_machine(input), do: | |
Enum.reduce( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day24 do | |
def part1(), do: | |
connections() |> all_bridges() |> Stream.map(&(&1.strength)) |> Enum.max() | |
def part2(), do: | |
(connections() |> all_bridges() |> Enum.max_by(&{&1.length, &1.strength})).strength | |
defp all_bridges(connections), do: | |
Enum.reduce(Map.get(connections, 0), [], &bridges(connections, {0, &1}, &2)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day23 do | |
defmodule Machine.Core do | |
def new(instructions), do: | |
%{ | |
registers: %{}, | |
position: 0, | |
instructions: instructions |> Enum.to_list() |> :array.from_list(fixed: true) | |
} | |
def done?(machine), do: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day22 do | |
def part1(), do: | |
count_infected(10_000, [:clean, :infected]) | |
def part2(), do: | |
count_infected(10_000_000, [:clean, :weakened, :infected, :flagged]) | |
defp count_infected(num_moves, state_transitions), do: | |
state_transitions |> new_virus() |> count_infected(num_moves, 0) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day21 do | |
def part1(), do: | |
enhancements() |> generate_art(5) |> count_pixels() | |
def part2(), do: | |
enhancements() |> generate_art(18) |> count_pixels() | |
defp count_pixels(grid), do: | |
Enum.count(for <<bit::1 <- grid>>, bit == 1, do: bit) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day20 do | |
def part1() do | |
{id, _particle} = Enum.min_by(particles(), &particle_weight/1) | |
id | |
end | |
def part2() do | |
particles = particles() | |
count_survivors(particles, sorted_collisions(particles)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day19 do | |
def part1(), do: | |
read_map() |> trip_steps() |> encountered_letters() |> Enum.to_list() | |
def part2(), do: | |
read_map() |> trip_steps() |> Enum.count() | |
defp encountered_letters(trip_steps), do: | |
trip_steps |> Stream.map(&Map.fetch!(&1.map, &1.pos)) |> Stream.reject(&(&1 in [?|, ?-, ?+])) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day18 do | |
defmodule Machine.Core do | |
def new(instructions), do: | |
%{ | |
registers: %{}, | |
position: 0, | |
instructions: instructions |> Enum.to_list() |> :array.from_list(fixed: true) | |
} | |
def done?(machine), do: |
NewerOlder