This file contains hidden or 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 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 |
This file contains hidden or 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 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) |
This file contains hidden or 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 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 |
This file contains hidden or 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
| # 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: |
This file contains hidden or 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
| # 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) |
This file contains hidden or 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
| # 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} = |
This file contains hidden or 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
| # 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() |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |