Skip to content

Instantly share code, notes, and snippets.

View rugyoga's full-sized avatar

Guy Argo rugyoga

  • San Francisco
View GitHub Profile
@rugyoga
rugyoga / solution.ex
Created January 30, 2024 09:55
003 Longest substring without repeated characters using binary
defmodule Solution do
defmodule Window do
defstruct [:max, :seen, :sub_offset, :sub_size, :string]
def new(s), do: %Window{max: 0, seen: MapSet.new, sub_offset: 0, sub_size: 0, string: s}
def add_back(window, char) do
%Window{window | seen: MapSet.put(window.seen, char), sub_size: window.sub_size+1, max: max(window.max, MapSet.size(window.seen)+1)}
end
@rugyoga
rugyoga / solution.ex
Created January 30, 2024 09:48
003 Longest string without repeated character using a Queue and char_list
defmodule Solution do
defmodule Queue do
defstruct [front: [], back: [], size: 0]
def push(q, item), do: %Queue{ q | size: q.size+1, back: [item | q.back]}
def pop(%Queue{size: 0} = q), do: {:empty, q}
def pop(%Queue{front: [a | as]} = q), do: {a, %Queue{q | front: as, size: q.size-1}}
def pop(%Queue{front: []} = q), do: pop(%Queue{q | front: Enum.reverse(q.back), back: []})
end
@rugyoga
rugyoga / mergesort.ex
Created December 21, 2023 21:48
mergesort
defmodule People do
@people ["Mary","John","Emma"]
@heights [180,165,170]
def sort_heights(names, heights) do
heights
|> Enum.zip(names)
|> mergesort()
|> Enum.unzip()
|> elem(1)
@rugyoga
rugyoga / AOC2023Day19.ex
Created December 19, 2023 23:28
Advent of Code 2023 Day 19
import AOC
aoc 2023, 19 do
def p1(input) do
[code, data] = input |> String.split("\n\n")
code = parse_code(code)
data
|> String.split("\n", trim: true)
|> Enum.map(&parse_data/1)
|> Enum.map(&process(code, :in, &1))
@rugyoga
rugyoga / AOC2023Day18.ex
Created December 18, 2023 17:02
Advent of Code 2023 Day 18
import AOC
aoc 2023, 18 do
def p1(input) do
input
|> parse()
|> chunker()
|> Enum.reduce({{0,0}, %{}}, &dig/2)
|> elem(1)
|> count_enclosed()
@rugyoga
rugyoga / AOC2023Day17.ex
Created December 17, 2023 07:05
Advent of Code 2023 Day 17
import AOC
aoc 2023, 17 do
def compute(input, candidates) do
{{max_row, max_col}, items} = Grid.parse(input)
heat_map =
items
|> Enum.map(fn {coord, number} -> {coord, String.to_integer(number)} end)
|> Map.new
Heap.new()
@rugyoga
rugyoga / AOC2023Day16.ex
Created December 16, 2023 07:25
ADvent of Code 2023 Day 16
import AOC
aoc 2023, 16 do
def p1(input) do
input
|> Grid.parse()
|> then(fn {max, grid} -> {max, Map.new(grid)} end)
|> compute({{0, 0}, :east})
end
@rugyoga
rugyoga / AOC2023Day15.ex
Created December 15, 2023 06:07
Advent of Code 2023 day 15
import AOC
aoc 2023, 15 do
def p1(input) do
input |> String.split(",", trim: true) |> Enum.map(&hash/1) |> Enum.sum()
end
def hash(string) do
string
|> to_charlist()
@rugyoga
rugyoga / AOC2023Day14.ex
Created December 15, 2023 04:45
Advent of code 2023 Day 14
import AOC
aoc 2023, 14 do
def p1(input) do
{height, _, items} = input |> grid()
items |> shift_north() |> compute_load(height)
end
def p2(input) do
{height, width, items} = input |> grid()
@rugyoga
rugyoga / AOC2023Day13.ex
Created December 13, 2023 07:43
Advent of Code 2023 Day 13
import AOC
aoc 2023, 13 do
def p1(input) do
{xs, ys} = input |> grids() |> Enum.map(&calculate/1) |> Enum.unzip()
combine = fn items -> items |> List.flatten() |> Enum.map(fn n -> (1 + n)/2 end) |> Enum.sum() end
100*combine.(xs) + combine.(ys)
end
def p2(input) do