Skip to content

Instantly share code, notes, and snippets.

@ignaciovazquez
ignaciovazquez / advent-1.ex
Created December 2, 2015 19:45
Advent of Code. Day 1.
{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)
@ignaciovazquez
ignaciovazquez / advent-2.ex
Created December 2, 2015 19:45
Advent of Code. Day 2.
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))
@ignaciovazquez
ignaciovazquez / advent-3.ex
Created December 4, 2015 13:20
Advent of Code. Day 3.
### 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
@ignaciovazquez
ignaciovazquez / advent-4.ex
Created December 4, 2015 13:53
Advent of Code. Day 4.
# 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
@ignaciovazquez
ignaciovazquez / advent-5.ex
Last active December 5, 2015 17:22
Advent of Code. Day 5.
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
@ignaciovazquez
ignaciovazquez / advent-7.ex
Last active December 7, 2015 22:44
Advent of Code. Day 7.
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)
@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)))
@ignaciovazquez
ignaciovazquez / advent-6-part1.ex
Last active December 8, 2015 00:14
Advent of Code. Day 6 - Part 1.
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)))
@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-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)