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
{lights, _} = Enum.reduce(File.stream!("input.txt"), {HashDict.new, 0}, fn line, {map, y} -> | |
{new_map, _} = Enum.reduce( (String.strip(line) |> String.codepoints), {map, 0}, fn c, {map, x} -> | |
cond do | |
{x, y} in [{0, 0}, {99, 0}, {0, 99}, {99, 99}] -> {Dict.put(map, {x, y}, 1), x + 1} | |
c == "#" -> {Dict.put(map, {x, y}, 1), x + 1} | |
c == "." -> {Dict.put(map, {x, y}, 0), x + 1} | |
end | |
end) | |
{HashDict.merge(map, new_map), y + 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 EggNog do | |
def part1(list, target) do | |
Enum.reduce(1..length(list), 0, fn (x, acc) -> | |
acc + (combinations(list, x) |> Enum.reduce(0, fn y, acc2 -> | |
if Enum.sum(y) == target, do: acc2+1, else: acc2 | |
end)) | |
end) | |
end | |
def part2(list, target) do |
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
machine_output = Enum.reduce(File.stream!("mfcsam-output.txt"), %{}, fn line, acc -> | |
[compound, amount | _] = String.split(line, [": ", "\n"]) | |
Dict.put(acc, compound, String.to_integer(amount)) | |
end) | |
scores = Enum.map(File.stream!("input.txt"), fn(line) -> | |
[name, params] = String.strip(line) |> String.split([": "], parts: 2) | |
params = params |> String.split([",", ": ", " "]) |> Enum.chunk(3,3, [" "]) | |
points = Enum.map(params, fn([compound, score, _]) -> |
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
values = Enum.reduce(File.stream!("input.txt"), [], fn line, acc -> | |
[_, _, a, _, _, b, _, _, c, _, _, d, _, _, e | _] = String.split(line, [" ", ",", "\n"]) | |
acc ++ [[String.to_integer(a), String.to_integer(b), String.to_integer(c), String.to_integer(d), String.to_integer(e)]] | |
end) | |
[a1, a2, a3, a4, a5] = Enum.at(values, 0) | |
[b1, b2, b3, b4, b5] = Enum.at(values, 1) | |
[c1, c2, c3, c4, c5] = Enum.at(values, 2) | |
[d1, d2, d3, d4, d5] = Enum.at(values, 3) |
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 Deer do | |
def loop({name, speed, fly_time, rest_time}) do | |
state = %{rest_time_left: 0, fly_time_left: fly_time, distance: 0, status: :flying, points: 0} | |
loop({name, speed, fly_time, rest_time}, state) | |
end | |
defp loop({name, speed, fly_time, rest_time}, state) do | |
receive do | |
:tick -> | |
case state.status do |
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 PasswordGen do | |
import String, only: [rjust: 3, contains?: 2] | |
def get_password_stream(start) do | |
Stream.unfold(start, fn password -> | |
char_list = to_char_list(password) |> Enum.reverse | |
{new_char_list, _} = Enum.map_reduce(char_list, :continue, fn(c, status) -> | |
cond do | |
status == :done -> {c, status} | |
c == ?z -> {'a', :continue} | |
c in [?h, ?n, ?k] -> {c + 2, :done} |
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
look_and_say = fn x -> | |
{str, last_digit, count} = Enum.reduce(String.codepoints(x), {"", "", 0}, fn digit, {total, last_digit, count} -> | |
if digit != last_digit and count != 0 do | |
{total <> to_string(count) <> last_digit, digit, 1} | |
else | |
{total, digit, count + 1} | |
end | |
end) | |
str <> to_string(count) <> last_digit | |
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) |
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
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))) |
NewerOlder