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
{steps, final_floor} = instructions |> String.codepoints |> Enum.map_reduce(0, fn (x, acc) -> | |
acc = case x do | |
"(" -> acc + 1 | |
")" -> acc - 1 | |
_ -> acc | |
end | |
{acc, acc} | |
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
result = Enum.reduce(tests, {0, 0}, fn (x, {paper, ribbon}) -> | |
[l, w, h] = String.split(x, "x") |> Enum.map(fn n -> | |
{number, _ } = Integer.parse(n) | |
number | |
end) | |
[a, b | _ ] = Enum.sort([l, w, h]) | |
paper = paper + (2*l*w + 2*w*h + 2*h*l) + (a * b) | |
ribbon = ribbon + (l * w * h) + ((a * 2) + (b * 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
### Part 1 | |
move = fn x, y, dir -> | |
case dir do | |
">" -> {x + 1, y} | |
"<" -> {x - 1, y} | |
"^" -> {x, y + 1} | |
"v" -> {x, y - 1} | |
end | |
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
# Part 1 | |
integer_stream = Stream.iterate(1, &(&1 + 1)) | |
result = Enum.find(integer_stream, fn n -> | |
case :crypto.hash(:md5, "bgvyzdsv" <> to_string(n)) |> Base.encode16 |> to_char_list do | |
[48, 48, 48, 48, 48 | _ ] -> n | |
_ -> false | |
end | |
end) | |
IO.puts 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
is_nice_string = fn (line) -> | |
line = String.strip(line) | |
result = line |> String.codepoints |> Enum.reduce {0, 0, 0, ""}, fn char, {vowels, doubles, invalid, last_char} -> | |
if last_char == char do | |
doubles = doubles + 1 | |
end | |
if char in ["a", "e", "i", "o", "u"] do | |
vowels = vowels + 1 | |
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 Gates do | |
use Bitwise | |
import String, only: [to_atom: 1] | |
def gate(state \\ %{}) do | |
receive do | |
{:print_state} -> IO.inspect state | |
{:set_type, type} -> state = Dict.put(state, :type, type) | |
{:set_input_1, pid} -> state = Dict.put(state, :input_1, pid) | |
{:set_input_2, pid} -> state = Dict.put(state, :input_2, pid) |
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
parse_command = fn command -> | |
{cmd, rest} = case command do | |
<<"turn on ", rest :: binary>> -> {:on, rest} | |
<<"turn off ", rest :: binary>> -> {:off, rest} | |
<<"toggle ", rest :: binary>> -> {:toggle, rest} | |
end | |
[x1y1, _, x2y2] = String.split(rest) | |
[x1, y1] = String.split(x1y1, ",") |> Enum.map(&(String.to_integer(&1))) | |
[x2, y2] = String.split(x2y2, ",") |> Enum.map(&(String.to_integer(&1))) |
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
parse_command = fn command -> | |
{cmd, rest} = case command do | |
<<"turn on ", rest :: binary>> -> {:on, rest} | |
<<"turn off ", rest :: binary>> -> {:off, rest} | |
<<"toggle ", rest :: binary>> -> {:toggle, rest} | |
end | |
[x1y1, _, x2y2] = String.split(rest) | |
[x1, y1] = String.split(x1y1, ",") |> Enum.map(&(String.to_integer(&1))) | |
[x2, y2] = String.split(x2y2, ",") |> Enum.map(&(String.to_integer(&1))) |
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 solution is just a joke -although it works great!- It's not viable for bigger set sizes. | |
defmodule Distances do | |
def find_best(locations, distances) do find(locations, distances, [], :infinity, 0, &(&1 < &2)) end | |
def find_worst(locations, distances) do find(locations, distances, [], 0, 0, &(&2 < &1)) end | |
def find(_, _, best_route, best_distance, 100_000, _) do | |
{best_route, best_distance} | |
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 Distances do | |
def permutations([]) do [[]] end | |
def permutations(list) do | |
for h <- list, t <- permutations(list -- [h]), do: [h | t] | |
end | |
def find_best(locations, distances) do find(locations, distances, &(&1<&2), :infinity) end | |
def find_worst(locations, distances) do find(locations, distances, &(&1>&2), 0) end | |
def find(locations, distances, compare_fn, initial_value) do | |
locations_perms = permutations(locations) |
OlderNewer