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 Day18 do | |
def read_input do | |
File.read!("input/day18.txt") | |
|> String.split("\n", trim: true) | |
end | |
def solve_part2(expr) do | |
cond do | |
String.contains?(expr, "(") -> | |
Regex.replace(~r/\(([^()]+)\)/, expr, fn _, expr -> solve_part2(expr) 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 Day17 do | |
def read_input do | |
File.read!("input/day17.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, 0}, el } 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 Day15 do | |
def play(%{ :history => history, :last_value => value, :last_index => index }) do | |
next_history = seen(history, value, index) | |
next_value = get_value(next_history, value) | |
%{ | |
:history => next_history, | |
:last_index => index + 1, | |
:last_value => next_value, |
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 VM14 do | |
use Bitwise | |
defstruct [:memory, :mask_bits_for_and, :mask_values_for_or] | |
def new do | |
%VM14{memory: %{}, mask_bits_for_and: 0, mask_values_for_or: 0, mask_str: "0"} | |
end | |
def update_mask(vm, mask_str) do | |
mask_bits_for_and = mask_str |
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 Day13 do | |
def read_input do | |
[start_time, bus_times] = File.read!("input/day13.txt") | |
|> String.split("\n", trim: true) | |
times = String.split(bus_times, ",", trim: true) | |
|> Enum.reject(&(&1 == "x")) | |
|> Enum.map(&String.to_integer/1) | |
[String.to_integer(start_time), times] |
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 Ship do | |
defstruct heading: 90, pos: { 0, 0 } | |
def distance(ship) do | |
Tuple.to_list(ship.pos) | |
|> Enum.map(&(abs(&1))) | |
|> Enum.sum | |
end | |
def move(ship, %{ "dir" => "L", "steps" => degrees}) 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 Day11 do | |
def read_input do | |
File.read!("input/day11.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}, el } 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 Day10 do | |
def read_input do | |
File.read!("input/day10.txt") | |
|> String.split("\n", trim: true) | |
|> Enum.map(&String.to_integer/1) | |
|> Enum.sort | |
end | |
def create_graph(lines) do | |
nodes = [0] ++ lines ++ [Enum.max(lines) + 3] |
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) |