Skip to content

Instantly share code, notes, and snippets.

View sasa1977's full-sized avatar

Saša Jurić sasa1977

View GitHub Profile
defmodule Day7 do
def part1(), do:
root(read_programs_map()).name
def part2() do
programs_map = read_programs_map()
{:error, {:unbalanced, unbalanced_info}} = total_weight(programs_map, root(programs_map))
unbalanced_info.program.weight + unbalanced_info.missing_weight
end
defmodule Day6 do
def part1(banks), do:
run_debugger(banks).steps
def part2(banks), do:
run_debugger(banks).cycle_size
defp run_debugger(banks), do:
debug_loop(banks, length(banks), %{}, 0)
defmodule Day5 do
def moves(instruction_changer), do:
File.stream!("input.txt")
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> maze_path(instruction_changer)
|> Enum.count()
def instruction_change_1(instruction), do: instruction + 1
# http://adventofcode.com/2017/day/4
defmodule Day4 do
def part1(), do:
passphrases()
|> Stream.map(&words/1)
|> Stream.reject(&any_duplicate?/1)
|> Enum.count()
def part2(), do:
# http://adventofcode.com/2017/day/3
defmodule Day3 do
require Integer
def part1(square) do
final_point =
memory_points()
|> Stream.drop_while(&(&1.square != square))
|> Enum.take(1)
# http://adventofcode.com/2017/day/2
defmodule Day2 do
def checksum1(rows), do:
rows
|> Enum.map(&row_checksum1/1)
|> Enum.sum()
defp row_checksum1(row) do
{min, max} =
# http://adventofcode.com/2017/day/1
defmodule Day1 do
def sum1(digits), do:
digits
|> Stream.concat(Enum.take(digits, 1))
|> Stream.chunk_every(2, 1, :discard)
|> Stream.filter(&match?([el, el], &1))
|> Stream.map(&hd(&1))
|> Enum.sum()
@sasa1977
sasa1977 / simple_one_for_one.ex
Created May 26, 2017 07:53
Simple one for one supervising multiple children
defmodule Worker1 do
def start_link() do
Task.start_link(fn ->
Stream.repeatedly(fn -> :rand.uniform(1000) end)
|> Stream.each(&:timer.sleep/1)
|> Stream.each(fn _ -> IO.puts "worker 1" end)
|> Stream.run()
end)
end
end
defmodule Fun2MsTest do
@compile {:parse_transform, :ms_transform}
def test do
match_spec = :ets.fun2ms(fn({:foo, bar} = el) when bar == :baz -> el end)
IO.inspect match_spec
end
end
Fun2MsTest.test
defmodule Primes do
# Basic implementation of an infinite prime generator, as explained at
# http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
def new do
%{composites: HashDict.new, current: 2}
end
def next(%{composites: composites, current: current} = sieve) do
case HashDict.get(composites, current) do