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 ElmdbStage do | |
@moduledoc """ | |
Create a GenStage for an :elmdb based LMDB database cursor. | |
This is for linearly reading from a db | |
""" | |
use GenStage | |
def start_link( env, db ) do | |
GenStage.start_link( __MODULE__, { env, db } ) | |
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 HashSignature do | |
@doc """ | |
encode input to a sha256 and output as an armored string. | |
results should be fully portable and consistent with other | |
tools. | |
""" | |
def get( input ) do | |
:crypto.hash( :sha256, input ) | |
|> :crypto.bytes_to_integer | |
|> (fn i -> :io_lib.format("~64.16.0b",[i]) |> hd 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 TimeAgent do | |
def start_link do | |
Agent.start_link( fn -> { Map.new, Map.new } end, name: __MODULE__ ) | |
end | |
def time_run( m, f, a ) do | |
{ us, result } = :timer.tc( m, f, a ) | |
Agent.cast( __MODULE__, __MODULE__, :add_sample, [ to_string(m) <> "." <> to_string(f), us ] ) | |
result |
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 TermRoundTrip do | |
@moduledoc """ | |
Convert a term to a readable string then back to a term | |
""" | |
def term_to_binary( term ) do | |
:io_lib.format("~p.~n", [ term ] ) | |
|> :erlang.iolist_to_binary | |
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 Day4 do | |
def parser( input ) do | |
String.split( input, "\n" ) | |
|> Enum.reduce( [], fn "", acc -> acc; line,acc -> [ String.split(line," ") | acc ] end ) | |
end | |
def start1( input ) do | |
parser( input ) |> step1a( [valid: 0, invalid: 0] ) | |
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 Day3 do | |
# This does more work than required but is still O(1) | |
# diagonal low right is even square root | |
def start1( val ) do | |
col_height = :math.sqrt( val ) |> Float.ceil |> trunc | |
col_height = if Integer.mod( col_height, 2 ) == 0, do: col_height+1, else: col_height | |
low_right_corner = :math.pow( col_height, 2 ) |> trunc | |
distance = low_right_corner - val | |
ring_length = col_height * 4 - 4 |
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 Day2 do | |
def parser( input ) do | |
String.split( input, "\n" ) | |
|> Enum.map( fn k -> | |
String.split( k, "\t") | |
|> Enum.map( &String.to_integer/1 ) | |
end ) | |
end | |
def start1( input ) do | |
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 TimeoutExample do | |
use GenServer | |
def init(_args), do: {:ok, 0, 5_000} | |
def handle_info(:timeout, state) do | |
IO.inspect(state) | |
{:noreply, state+1, 5_000 + state*1000} | |
end | |
end | |
iex> {:ok, p} = GenServer.start(TimeoutExample, []) |