Skip to content

Instantly share code, notes, and snippets.

View mrdougwright's full-sized avatar
🏠
Working from home

Doug Wright mrdougwright

🏠
Working from home
View GitHub Profile
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 ->
@mrdougwright
mrdougwright / day2.exs
Created December 2, 2023 18:59
AoC 2023 - Day 2
# 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 ->
@mrdougwright
mrdougwright / day1.exs
Created December 2, 2023 18:57
AoC 2023 - Day 1
{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",
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)
@mrdougwright
mrdougwright / scrub.py
Created March 25, 2020 00:36
versus scrubber
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 = []
@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
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":
@mrdougwright
mrdougwright / rps.exs
Created September 6, 2019 23:25
rock, paper, scissors
# 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)
$ iex
> :ets.new(:table, [:named_table])
@mrdougwright
mrdougwright / snakeit.rb
Created July 20, 2018 18:42
snake maker
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