Skip to content

Instantly share code, notes, and snippets.

@gungorkocak
gungorkocak / ui_helpers.ex
Created September 11, 2022 08:10
Small utility classes to work css for the new function component API in Phoenix LiveView >= 0.18
defmodule UI.Helpers do
@moduledoc """
Provides common helpers and js commands to work with
UI components.
"""
@doc """
Utility function that pipes into `assigns` and
helps with class concatenation inside live_views
@gungorkocak
gungorkocak / 11_chronal_charge.ex
Created October 24, 2019 12:43
Various Days from Advent of Code Challenge 2018 / with Elixir
defmodule Day11 do
@ets_table_name :day11_max_powers
@grid {300, 300}
@doc """
Initializes required partial sum caching mechanism (ets table).
"""
def start() do
:ets.new(@ets_table_name, [:set, :named_table, read_concurrency: true])
end
@gungorkocak
gungorkocak / 12_subterranean_sustainability.ex
Last active December 12, 2018 17:05
#AdventOfCode 2018 #elixir #elixirlang
defmodule Day12 do
@initial_state "##.#....#..#......#..######..#.####.....#......##.##.##...#..#....#.#.##..##.##.#.#..#.#....#.#..#.#"
@doc """
Resolves part#1.
"""
@spec index_sum_after_iteration(pos_integer) :: pos_integer
def index_sum_after_iteration(iteration \\ 20) do
last_state = iterate(@initial_state, iteration)
to_sum(last_state)
@gungorkocak
gungorkocak / 11_chronal_charge.ex
Created December 11, 2018 18:28
#AdventOfCode 2018 #elixir #elixirlang
defmodule Day11 do
@ets_table_name :day11_max_powers
@grid {300, 300}
@doc """
Initializes required partial sum caching mechanism (ets table).
"""
def start() do
:ets.new(@ets_table_name, [:set, :named_table, read_concurrency: true])
end
@gungorkocak
gungorkocak / 6_chronal_coordinates.ex
Last active December 6, 2018 23:00
#AdventOfCode 2018 #elixir #elixirlang
defmodule ChronalCoordinates do
def calc_part2(str_coords, threshold) do
coords = parse_coords(str_coords)
{bound_x, bound_y} = find_boundaries(coords)
reduce_grid({bound_x, bound_y}, 0, fn x, y, acc ->
sum = Enum.reduce(coords, 0, &sum_of_distances(&1, &2, {x, y}))
if sum < threshold, do: acc + 1, else: acc
end)
end
@gungorkocak
gungorkocak / 5_alchemical_reduction.ex
Last active December 5, 2018 15:43
#AdventOfCode 2018 #elixir #elixirlang
defmodule AlchemicalReduction do
@doc """
For example, again using the polymer dabAcCaCBAcCcaDA from above:
* Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
* Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
* Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
* Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
In this example, removing all C/c units was best, producing the answer 4.
@gungorkocak
gungorkocak / 4_repose_record.ex
Last active December 5, 2018 15:43
#AdventOfCode 2018 #elixir #elixirlang
defmodule ReposeRecord do
def calc_part1(logs_str) do
logs_str
|> String.split("\n", trim: true)
|> sort_logs()
|> Enum.map(&parse_event/1)
|> chart_event_timeline()
|> gather_guard_stats()
|> calc_weakest_guards_most_slept_metric()
end
@gungorkocak
gungorkocak / 3_spoils_of_fabric.ex
Created December 3, 2018 22:14
#AdventOfCode 2018 #elixir #elixirlang
defmodule SpoilsOfFabric do
def count_overlaps(stream) do
stream
|> Stream.map(&cleanup/1)
|> Stream.map(&to_grid_positions/1)
|> Enum.reduce(%{}, &count_positions/2)
|> Enum.reduce(0, &count_overlaps/2)
end
def detect_sacred_fabric(stream) do
@gungorkocak
gungorkocak / 2_inventory_management.ex
Last active December 2, 2018 21:12
#AdventOfCode 2018 #elixir #elixirlang
defmodule InventoryManagement.Checksum do
def checksum(stream) do
stream
|> Stream.map(&String.trim/1)
|> Stream.map(&count_occurances/1)
|> Enum.reduce([two: 0, three: 0], &collect_each/2)
|> Enum.reduce(1, fn {_, val}, acc -> val * acc end)
end
defp collect_each([two: item_two, three: item_three], two: two, three: three) do
@gungorkocak
gungorkocak / 1_chronal_calibration.ex
Last active December 2, 2018 21:01
#AdventOfCode 2018 #elixir #elixirlang
defmodule ChronalCalibration do
def calc_result(stream), do: Enum.sum(stream)
def find_first_redundant(stream) do
Stream.cycle(stream)
|> Stream.scan({0, MapSet.new([0])}, &collect_step/2)
|> Stream.filter(&redundant?/1)
|> Stream.map(fn {elem, _} -> elem end)
|> Enum.take(1)
|> Enum.at(0)