Skip to content

Instantly share code, notes, and snippets.

View rugyoga's full-sized avatar

Guy Argo rugyoga

View GitHub Profile
@rugyoga
rugyoga / AOC2023Day02.ex
Last active December 2, 2023 23:28
Advent of Code 2023 Day 2
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
import AOC
aoc 2023, 1 do
@moduledoc """
https://adventofcode.com/2023/day/1
"""
def calibration(input) do
input
|> String.split("", trim: true)
/* 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;
@rugyoga
rugyoga / tree.ex
Created September 7, 2022 17:28
Tree example
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)}
@rugyoga
rugyoga / iex_example.ex
Last active September 7, 2022 17:31
iex example
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}}}]
@rugyoga
rugyoga / github.ex
Created September 6, 2022 17:34
Module to wrangle GitHub URLs.
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()
@rugyoga
rugyoga / file_cache.ex
Created September 6, 2022 17:30
Agent that performs file cacheing
defmodule Audit.FileCache do
@moduledoc """
Simple File cache Agent
"""
use Agent
def start_link(_args) do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
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
@rugyoga
rugyoga / changelist.ex
Created September 5, 2022 03:31
Change list
@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: []
@rugyoga
rugyoga / audit_macro.ex
Created September 5, 2022 03:26
Audit macro
defmacro audit(record) do
quote generated: true do
unquote(__MODULE__).audit_fun(unquote(record), __ENV__)
end
end