Skip to content

Instantly share code, notes, and snippets.

View sasa1977's full-sized avatar

Saša Jurić sasa1977

View GitHub Profile
# 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:
#
@sasa1977
sasa1977 / sql_parser.exs
Created October 13, 2019 08:56
Basic SQL parser developed at WebCamp Zagreb, 2019
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))
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(
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))
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:
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)
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)
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
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 [?|, ?-, ?+]))
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: