From 922d0971d3263620118b25244829c817005d268a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kleszczy=C5=84ski?= Date: Mon, 9 Dec 2024 22:32:03 +0100 Subject: [PATCH] back to learning elixir --- 2021/01/01.exs | 20 --------- 2021/{01 => d01}/input.txt | 0 2021/d01/main.exs | 26 ++++++++++++ 2021/{02 => d02}/input.txt | 0 2021/{02/02.exs => d02/main.exs} | 0 2021/{03 => d03}/input.txt | 0 2021/{03/03.exs => d03/main.exs} | 0 2021/{03/test.txt => d03/test-runs/0.txt} | 0 2021/framework/framework.exs | 51 +++++++++++++++++++++++ package.json | 1 + 10 files changed, 78 insertions(+), 20 deletions(-) delete mode 100644 2021/01/01.exs rename 2021/{01 => d01}/input.txt (100%) create mode 100644 2021/d01/main.exs rename 2021/{02 => d02}/input.txt (100%) rename 2021/{02/02.exs => d02/main.exs} (100%) rename 2021/{03 => d03}/input.txt (100%) rename 2021/{03/03.exs => d03/main.exs} (100%) rename 2021/{03/test.txt => d03/test-runs/0.txt} (100%) create mode 100644 2021/framework/framework.exs diff --git a/2021/01/01.exs b/2021/01/01.exs deleted file mode 100644 index 4d02868..0000000 --- a/2021/01/01.exs +++ /dev/null @@ -1,20 +0,0 @@ -lines = File.stream!("input.txt") - |> Stream.map(&String.trim/1) - |> Stream.map(&String.to_integer/1) - |> Enum.to_list - -# Part 1 -count = lines - |> Enum.chunk_every(2, 1, :discard) - |> Enum.count(fn [a, b] -> a < b end) - -IO.puts("Part 1: #{count}") - -# Part 2 -count = 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) - -IO.puts("Part 2: #{count}") diff --git a/2021/01/input.txt b/2021/d01/input.txt similarity index 100% rename from 2021/01/input.txt rename to 2021/d01/input.txt diff --git a/2021/d01/main.exs b/2021/d01/main.exs new file mode 100644 index 0000000..7d13e6c --- /dev/null +++ b/2021/d01/main.exs @@ -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) diff --git a/2021/02/input.txt b/2021/d02/input.txt similarity index 100% rename from 2021/02/input.txt rename to 2021/d02/input.txt diff --git a/2021/02/02.exs b/2021/d02/main.exs similarity index 100% rename from 2021/02/02.exs rename to 2021/d02/main.exs diff --git a/2021/03/input.txt b/2021/d03/input.txt similarity index 100% rename from 2021/03/input.txt rename to 2021/d03/input.txt diff --git a/2021/03/03.exs b/2021/d03/main.exs similarity index 100% rename from 2021/03/03.exs rename to 2021/d03/main.exs diff --git a/2021/03/test.txt b/2021/d03/test-runs/0.txt similarity index 100% rename from 2021/03/test.txt rename to 2021/d03/test-runs/0.txt diff --git a/2021/framework/framework.exs b/2021/framework/framework.exs new file mode 100644 index 0000000..ca46156 --- /dev/null +++ b/2021/framework/framework.exs @@ -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 diff --git a/package.json b/package.json index d64d92a..ca59919 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "scripts": { "aoc": "tsx", "dotnet": "dotnet run --project", + "elixir": "elixir", "postinstall": "husky", "prepare": "husky" },