Skip to content

Instantly share code, notes, and snippets.

module Flatr
def self.flat(array)
Flatr._flat(array, [])
end
def self._flat(array, acc)
if array.kind_of?(Array)
# Check if we reached the end
if array.empty?
return acc
defmodule Dec1 do
@input "1122"
def solve do
@input
|> String.codepoints
|> Enum.map(&(String.to_integer(&1)))
|> solve
end
@marjaimate
marjaimate / control_tower_make_landing.ex
Last active October 30, 2017 09:34
Control Tower - minimal setup with gen_server. We can open an airport, open a number of landing strips ( so we can land planes on it later! ) and close the airport.
def handle_call({:land_plane, %{:flight_number => flight_number} = plane, %{:id => id} = ls}, from, %{:airport => airport} = landingstrips) do
IO.puts("[TOWER][#{airport}] Plane #{flight_number} approaching runway #{id} ~n")
GenServer.cast(self(), {:make_landing, plane, ls, from})
{:reply, :ok, landingstrips}
end
def handle_cast({:make_landing, %{:flight_number => flight_number}, %{:id => ls_id} = ls, {plane, _} = _from}, %{:airport => airport} = landingstrips) do
:timer.sleep(300)
IO.puts("[TOWER][#{airport}] Plane #{flight_number} landed, freeing up runway #{ls_id}")
@marjaimate
marjaimate / product.ex
Created September 8, 2017 05:40
Euler - problem 8
number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
defmodule Pr
⍝ Setup the string
x←'(aa(ccc)sss(fff)ggg)sss(www)'
⍝ Define a function to get the left parens as 1 and the right parens as ¯1
parens ← {(⍵='(') - (⍵=')')}
parens x
⍝ 1 0 0 1 0 0 0 ¯1 0 0 0 1 0 0 0 ¯1 0 0 0 ¯1 0 0 0 1 0 0 0 ¯1
⍝ To test for the maximum just use scan operator with addition to get the nesting levels
+\ parens x
"KUUUUUBS"
|> String.codepoints
|> List.foldl([], fn(char, [{n, char} | tail]) -> [{n + 1, char} | tail]; (char, acc) -> [{1, char}] ++ acc end)
|> List.foldl(<<>>, fn({occ, char}, acc) -> Integer.to_string(occ) <> char <> acc end)
# "1K5U1B1S"
defmodule Runlength do
# Encode
def encode(<< first :: binary-size(1), rest :: binary >> ) do
encode(rest, first, 1, "")
end
def encode(<<>>, current_letter, counter, result) do
<<result :: binary, (Integer.to_string(counter)) :: binary, current_letter :: binary >>
end
@marjaimate
marjaimate / roman_num.exs
Created March 23, 2017 20:53
Roman number conversions
defmodule Roman do
def append_roman(base, count, character) do
base <> String.pad_trailing("", count, character)
end
def breakdown(number, denominator) do
{div(number, denominator), rem(number, denominator)}
end
def convert(number) do
require 'net/http'
require 'json'
namespace :currency do
desc "Get latest exchange rates"
task get_rates: :environment do
URL = "http://your.exchange.api.com/rate?from=%FROM%&to=%TO%"
OUT_PATH = "./rates.json"
output = []
currencies = Currency.pluck(:iso_code)