change directory names
This commit is contained in:
parent
3cff0fc5cf
commit
78b8881016
90 changed files with 0 additions and 0 deletions
17
21/lib/days/day1.ex
Normal file
17
21/lib/days/day1.ex
Normal file
|
@ -0,0 +1,17 @@
|
|||
defmodule AOC.Day1 do
|
||||
def part1 do
|
||||
AOC.Util.input_integers(1, 1)
|
||||
|> Enum.chunk_every(2, 1, :discard)
|
||||
|> Enum.map(fn [x, y] -> y > x end)
|
||||
|> Enum.count(& &1)
|
||||
end
|
||||
|
||||
def part2 do
|
||||
AOC.Util.input_integers(1, 1)
|
||||
|> Enum.chunk_every(3, 1, :discard)
|
||||
|> Enum.map(&Enum.sum/1)
|
||||
|> Enum.chunk_every(2, 1, :discard)
|
||||
|> Enum.map(fn [x, y] -> y > x end)
|
||||
|> Enum.count(& &1)
|
||||
end
|
||||
end
|
27
21/lib/days/day2.ex
Normal file
27
21/lib/days/day2.ex
Normal file
|
@ -0,0 +1,27 @@
|
|||
defmodule AOC.Day2 do
|
||||
def parse_input do
|
||||
AOC.Util.input_lines(2, 1)
|
||||
|> Enum.map(fn l ->
|
||||
[dir, amount] = String.split(l, " ")
|
||||
{String.to_atom(dir), String.to_integer(amount)}
|
||||
end)
|
||||
end
|
||||
|
||||
def part1 do
|
||||
parse_input()
|
||||
|> Enum.reduce({0, 0}, fn
|
||||
{:forward, amount}, {distance, depth} -> {distance + amount, depth}
|
||||
{:down, amount}, {distance, depth} -> {distance, depth + amount}
|
||||
{:up, amount}, {distance, depth} -> {distance, depth - amount}
|
||||
end)
|
||||
end
|
||||
|
||||
def part2 do
|
||||
parse_input()
|
||||
|> Enum.reduce({0, 0, 0}, fn
|
||||
{:down, amount}, {distance, depth, aim} -> {distance, depth, aim + amount}
|
||||
{:up, amount}, {distance, depth, aim} -> {distance, depth, aim - amount}
|
||||
{:forward, amount}, {distance, depth, aim} -> {distance + amount, depth + aim * amount, aim}
|
||||
end)
|
||||
end
|
||||
end
|
21
21/lib/util.ex
Normal file
21
21/lib/util.ex
Normal file
|
@ -0,0 +1,21 @@
|
|||
defmodule AOC.Util do
|
||||
def input_file_name(day, input) do
|
||||
Path.join([File.cwd!(), "inputs", "day#{day}", "input#{input}.txt"])
|
||||
end
|
||||
|
||||
def input_file(day, input) do
|
||||
input_file_name(day, input)
|
||||
|> File.read!()
|
||||
end
|
||||
|
||||
def input_lines(day, input) do
|
||||
input_file_name(day, input)
|
||||
|> File.stream!()
|
||||
|> Enum.map(&String.trim/1)
|
||||
end
|
||||
|
||||
def input_integers(day, input) do
|
||||
input_lines(day, input)
|
||||
|> Enum.map(&String.to_integer/1)
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue