$ iex
> :ets.new(:table, [:named_table])
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 Day3 do | |
def read_input(file) do | |
file | |
|> File.read!() | |
|> String.split("\n", trim: true) | |
|> Enum.map(&String.split(&1, "", trim: true)) | |
end | |
def p1(input) do | |
Enum.with_index(input, fn line, row_index -> |
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
# https://adventofcode.com/2023/day/2/input | |
{input, _} = Code.eval_file(File.cwd!() <> "/data/day-02-string.exs") | |
defmodule Day2 do | |
@bag %{"red" => 12, "green" => 13, "blue" => 14} | |
def p1(input) do | |
input | |
|> String.split("\n", trim: true) | |
|> Enum.reduce(0, fn game, acc -> |
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
{input, _} = Code.eval_file(File.cwd!() <> "/data/day-01.exs") | |
defmodule Day1 do | |
@word_ints %{ | |
"0" => "0", | |
"1" => "1", | |
"2" => "2", | |
"3" => "3", | |
"4" => "4", | |
"5" => "5", |
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 bucket_items_applies?(discount) | |
@application_count = 0 | |
discount.bucket.bucket_items.all.each do |bucket_item| | |
@cloned_items = items.map(&:clone) | |
matching_line_item_product_index = find_line_item_product_index(bucket_item) | |
return false if matching_line_item_product_index.blank? | |
allocate_discount(matching_line_item_product_index: matching_line_item_product_index, discount: discount, bucket_item: bucket_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
def scrub(data): | |
if type(data) is list: | |
return scrub_list(data) | |
elif type(data) is dict: | |
return scrub_obj(data) | |
else: | |
return data | |
def scrub_list(data): | |
new_list = [] |
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 meetup(pos_integer, pos_integer, weekday, schedule) :: :calendar.date() | |
def meetup(year, month, weekday, schedule) do | |
week = %{monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7} | |
week_multi = %{first: 0, second: 1, third: 2, fourth: 3, last: 4} | |
{:ok, first_date} = Date.new(year, month, 1) | |
beg_mo = Date.day_of_week(first_date) | |
day = week[weekday] | |
# if first day of month has surpassed target day, add 7 to get to nearest day |
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
const update = (items) => { | |
items.map(update_item) | |
} | |
const update_item = (item) => { | |
switch (item.name) { | |
case "Sulfuras, Hand of Ragnaros": | |
return item | |
break | |
case "Aged Brie": |
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
# Rock, Paper, Scissors | |
# n = number of players | |
# players = string of letters "RPSPSSR" | |
defmodule RPS do | |
@winners %{"R" => "S", "S" => "P", "P" => "R"} | |
def find_winners(n, players) do | |
chars = String.split(players, "", 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
def snakeit(sentence) | |
sentence.split.map do |word| | |
word.split("").map do |letter| | |
up_or_down(letter) | |
end.join() | |
end.join(" ") | |
end | |
def up_or_down(letter) | |
random_bool(letter) ? letter.upcase : letter.downcase |
NewerOlder