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 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 |
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 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) |
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 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, "."))) |
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 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))) |
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 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 |
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 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) |
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 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 |
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 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 |
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 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 |
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
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: |