Skip to content

Commit

Permalink
back to learning elixir
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Dec 9, 2024
1 parent 806c2d8 commit 922d097
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 20 deletions.
20 changes: 0 additions & 20 deletions 2021/01/01.exs

This file was deleted.

File renamed without changes.
26 changes: 26 additions & 0 deletions 2021/d01/main.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Code.require_file("2021/framework/framework.exs")

defmodule Aoc.Day01 do
def reader(filePath) do
File.stream!(filePath)
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> Enum.to_list
end

def part1(lines) do
lines
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [a, b] -> a < b end)
end

def part2(lines) do
lines
|> Enum.chunk_every(3, 1, :discard)
|> Enum.map(&Enum.sum/1)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [a, b] -> a < b end)
end
end

Aoc.Core.run_solution("01", &Aoc.Day01.reader/1, &Aoc.Day01.part1/1, &Aoc.Day01.part2/1)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions 2021/framework/framework.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Aoc.Core do
def build_string(type, label, part, result, duration) do
"#{type} #{String.pad_trailing(label, 20, " ")} #{part} #{String.pad_trailing("#{result}", 15, " ")} #{duration}"
end

def example_part1(result, test_file_name, duration_in_ms) do
IO.puts(
IO.ANSI.yellow <> build_string("[EXM]", "(#{test_file_name})", "Part 1:", result, "#{duration_in_ms}ms")
)
end

def example_part2(result, test_file_name, duration_in_ms) do
IO.puts(
IO.ANSI.red <> build_string("[EXM]", "(#{test_file_name})", "Part 1:", result, "#{duration_in_ms}ms")
)
end

def solution_part1(result, duration_in_ms) do
IO.puts(
IO.ANSI.green <> build_string("[SLN]", "", "Part 1:", result, "#{duration_in_ms}ms")
)
end

def solution_part2(result, duration_in_ms) do
IO.puts(
IO.ANSI.black <> build_string("[SLN]", "", "Part 1:", result, "#{duration_in_ms}ms")
)
end

def run_example do

end

def run_solution(day, reader, part1, part2) do
filePath = "2021/d#{day}/input.txt"
data = reader.(filePath)

# TODO: Timing
resultPart1 = part1.(data)
solution_part1(resultPart1, 0)

resultPart2 = part2.(data)
solution_part1(resultPart2, 0)

# {timePart1, resultPart1} = :timer.tc(part1, data)
# solution_part1(resultPart1, timePart1)

# {timePart2, resultPart2} = :timer.tc(part2, data)
# solution_part2(resultPart2, timePart2)
end
end
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"aoc": "tsx",
"dotnet": "dotnet run --project",
"elixir": "elixir",
"postinstall": "husky",
"prepare": "husky"
},
Expand Down

0 comments on commit 922d097

Please sign in to comment.