Skip to content

Instantly share code, notes, and snippets.

defmodule Day9 do
@preamble_length 25
def read_input do
File.read!("input/day9.txt")
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
end
def valid?(window, item) do
defmodule VM do
defstruct [:ip, :code, :acc]
end
defmodule Day8 do
def read_input do
File.read!("input/day8.txt")
|> String.split("\n", trim: true)
|> Enum.map(&(String.split(&1, " ", trim: true)))
|> Enum.map(fn [action, param] -> { action, String.to_integer(param) } end)
defmodule Rule do
defstruct [:from, :to, :count]
end
defmodule Day7 do
def read_input do
File.read!("input/day7.txt")
|> String.split("\n", trim: true)
|> Enum.map(&(String.replace(&1, "bags", "bag")))
|> Enum.map(&(String.trim(&1, ".")))
defmodule Day6 do
def read_input do
File.read!("input/day6.txt")
|> String.split("\n\n", trim: true)
end
def get_common_answers(str) do
answers = String.split(str, "\n", trim: true)
|> Enum.map(&String.graphemes/1)
|> Enum.map(&(MapSet.new(&1)))
defmodule Seat do
defstruct min_row: 0, max_row: 127, min_col: 0, max_col: 7
def from_string(str) do
str
|> String.graphemes
|> Enum.reduce(%Seat{}, &Day5.process_letter/2)
end
def from_tuple({row, col}) do
defmodule Day4 do
def read_input do
File.read!("input/day4.txt")
|> String.split("\n\n", trim: true)
|> Enum.map(&to_passport/1)
end
def to_passport(str) do
str
|> String.split(~r{\s+}, trim: true)
defmodule Day3 do
def read_input do
File.read!("input/day3.txt")
|> String.split("\n", trim: true)
|> Enum.map(&String.graphemes/1)
|> Enum.map(&Enum.with_index/1)
|> Enum.with_index
|> Enum.flat_map(fn {line, row} -> Enum.map(line, fn {el, col} -> {{row, col}, (if el == "#", do: 1, else: 0) } end) end)
|> Enum.into(%{})
end
defmodule Day2 do
def read_input do
File.read!("input/day2.txt")
|> String.split("\n", trim: true)
|> Enum.map(&(Regex.scan(~r/(\d+)-(\d+) (\w+): (.*)/, &1)))
|> Enum.map(fn [[_, min, max, char, text]] -> {
String.to_integer(min),
String.to_integer(max),
char,
text
defmodule Day1 do
def read_input do
File.read!("input/day1.txt")
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
end
def part1 do
values = read_input()
for a <- values, b <- values, a + b == 2020, do: a * b
text = "some string (with parentheses (and also inner parentheses)) (yay) hi"
stack = []
for i in range(len(text)):
char = text[i]
if char == "(":
stack.append(char)
elif char == ")":
stack.pop()
if len(stack) == 0: