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
import AOC | |
import String, only: [split: 2, to_integer: 1] | |
import Enum, only: [map: 2, max: 1, product: 1, reduce: 3, sum: 1, zip_with: 2] | |
aoc 2023, 2 do | |
def parse_game(line) do | |
["Game " <> id, hands] = split(line, ": ") | |
{to_integer(id), hands |> split("; ") |> map(&parse_hand/1)} | |
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
import AOC | |
aoc 2023, 1 do | |
@moduledoc """ | |
https://adventofcode.com/2023/day/1 | |
""" | |
def calibration(input) do | |
input | |
|> String.split("", trim: true) |
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
/* stackless version */ | |
var flatten = function(head) { | |
const first = head; | |
var stack = null; | |
while (head != null) { | |
if (head.child != null) { | |
if (head.next != null) { | |
head.next.prev = stack; | |
stack = head.next; |
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 Tree do | |
alias __MODULE__ | |
use Audit | |
defstruct left: nil, item: nil, right: nil, __audit_trail__: nil | |
def add(nil, item), do: %Tree{item: item} | |
def add(tree = %Tree{left: l, item: i, right: r}, item) do | |
cond do | |
item < i -> %Tree{audit(tree) | left: add(l, item)} |
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
iex> nil |> Tree.add(2) |> Tree.add(1) |> Tree.add(3) |> Audit.to_string() |> IO.puts | |
github url: https://github.com/rugyoga/tree_ex/tree/main/lib/tree.ex#L15 | |
local path: lib/tree.ex:15 | |
code: item > i -> %Tree{audit(tree) | right: add(r, item)} | |
diff: [{[:right], {:add, %Tree{item: 3, left: nil, right: nil}}}] | |
===== | |
github url: https://github.com/rugyoga/tree_ex/tree/main/lib/tree.ex#L14 | |
local path: lib/tree.ex:14 | |
code: item < i -> %Tree{audit(tree) | left: add(l, item)} | |
diff: [{[:left], {:add, %Tree{item: 1, left: nil, right: nil}}}] |
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 Audit.Github do | |
@moduledoc """ | |
Helper functions for generating Github URLs from filename and line number | |
""" | |
@type line_number_t :: binary | non_neg_integer() | |
@type filename_t :: binary() | |
@type git_t :: binary() | nil | |
@spec git([binary()]) :: git_t() |
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 Audit.FileCache do | |
@moduledoc """ | |
Simple File cache Agent | |
""" | |
use Agent | |
def start_link(_args) do | |
Agent.start_link(fn -> %{} end, name: __MODULE__) | |
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
def to_string(r) do | |
r | |
|> changelist() | |
|> Enum.map_join("\n=====\n", &stringify_change/1) | |
end | |
@spec changelist(term) :: [change_t()] | |
defp changelist(r = %_{__audit_trail__: audit_trail}) do | |
if audit_trail, do: [{r, audit_trail} | changelist(record(audit_trail))], else: [] | |
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
@spec changelist(term) :: [change_t()] | |
defp changelist(r = %_{__audit_trail__: audit_trail}) do | |
if audit_trail, do: [{r, audit_trail} | changelist(record(audit_trail))], else: [] | |
end | |
defp changelist(_), 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
defmacro audit(record) do | |
quote generated: true do | |
unquote(__MODULE__).audit_fun(unquote(record), __ENV__) | |
end | |
end |