Skip to content

Instantly share code, notes, and snippets.

Saša Jurić sasa1977

Block or report user

Report or block sasa1977

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@sasa1977
sasa1977 / sql_parser.exs
Created Oct 13, 2019
Basic SQL parser developed at WebCamp Zagreb, 2019
View sql_parser.exs
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))
View aoc2017_day25.ex
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(
View aoc2017_day24.ex
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))
View aoc2017_day23.ex
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:
View aoc2017_day22.ex
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)
View aoc2017_day21.ex
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)
View aoc2017_day20.ex
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
View aoc2017_day19.ex
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 [?|, ?-, ?+]))
View aoc2017_day18.ex
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:
View aoc2017_day17.ex
defmodule Day17 do
def part1(num_steps), do:
insert(2017, num_steps, [], &List.insert_at/3)
|> Stream.chunk_every(2, 1)
|> Enum.find(&match?([2017, _], &1))
|> Enum.at(1)
def part2(num_steps), do:
insert(50_000_000, num_steps, nil,
fn
You can’t perform that action at this time.