This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
struct tree | |
{ | |
struct tree *left; | |
int value; | |
struct tree *right; | |
}; |
This file contains 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 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 |
This file contains 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 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 |
This file contains 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 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) |
This file contains 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, 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)) |
This file contains 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, 18 do | |
def p1(input) do | |
input | |
|> parse() | |
|> chunker() | |
|> Enum.reduce({{0,0}, %{}}, &dig/2) | |
|> elem(1) | |
|> count_enclosed() |
This file contains 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, 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() |
This file contains 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, 16 do | |
def p1(input) do | |
input | |
|> Grid.parse() | |
|> then(fn {max, grid} -> {max, Map.new(grid)} end) | |
|> compute({{0, 0}, :east}) | |
end |
This file contains 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, 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() |
This file contains 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, 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() |
NewerOlder