Skip to content

Instantly share code, notes, and snippets.

@ignaciovazquez
ignaciovazquez / advent-18.ex
Created December 18, 2015 20:26
Advent of Code, Day 18.
{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)
@ignaciovazquez
ignaciovazquez / advent-17.ex
Created December 17, 2015 16:23
Advent of Code, Day 17.
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
@ignaciovazquez
ignaciovazquez / advent-16-part2.ex
Last active December 16, 2015 14:45
Advent of Code, Day 16
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, _]) ->
@ignaciovazquez
ignaciovazquez / advent-15-part2.ex
Last active December 15, 2015 15:49
Advent of Code, Day 15 - Part 2.
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)
@ignaciovazquez
ignaciovazquez / advent-14-part2.ex
Last active December 14, 2015 15:08
Advent of Code, Day 14 - Part 2.
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
@ignaciovazquez
ignaciovazquez / advent-11.ex
Last active December 11, 2015 15:45
Advent of Code. Day 11.
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}
@ignaciovazquez
ignaciovazquez / advent-10.ex
Created December 10, 2015 16:38
Advent of Code. Day 10.
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
@ignaciovazquez
ignaciovazquez / advent-9.ex
Last active December 9, 2015 15:50
Advent of Code. Day 9.
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)
@ignaciovazquez
ignaciovazquez / advent-9-heuristic.ex
Last active December 9, 2015 15:34
Advent of Code. Day 9 (heuristic solution)
# 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
@ignaciovazquez
ignaciovazquez / advent-6-part2.ex
Last active December 8, 2015 00:14
Advent of Code. Day 6 - Part 2.
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)))