Compare commits

..

1 Commits

Author SHA1 Message Date
bluepython508 89ded18386 Infra 2023-12-01 18:02:29 +00:00
9 changed files with 11 additions and 68 deletions
-2
View File
@@ -1,4 +1,2 @@
defmodule Aoc2023.Common do
def stringify(s) when is_binary(s) do s end
def stringify(n) when is_integer(n) do inspect(n) end
end
+2 -36
View File
@@ -2,48 +2,14 @@ defmodule Aoc2023.Day1 do
use Aoc2023
def parse(input) do
fn (replacement) ->
input
|> String.split("\n")
|> Enum.map(replacement)
|> Enum.map(fn (line) ->
line
|> String.to_charlist()
|> Enum.filter(&(?0 <= &1 && &1 <= ?9))
|> Enum.map(&(&1 - ?0))
end)
|> Enum.map(&[List.first(&1), List.last(&1)])
|> Enum.filter(fn (l) -> Enum.all?(l, &(&1 != nil)) end)
|> Enum.map(&Integer.undigits/1)
|> Enum.sum
end
end
def part1(input) do
input.(fn line -> line end)
nil
end
@numbers [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
]
defp replacements(line) do
line
|> String.replace(@numbers, fn (n) -> Integer.to_string(Enum.find_index(@numbers, &(&1 == n)) + 1) <> String.slice(n, 1..String.length(n)) end)
end
defp replacements_rec(line) do
if replacements(line) == line, do: line, else: replacements_rec(replacements(line))
end
def part2(input) do
input.(&replacements_rec/1)
nil
end
end
+7 -13
View File
@@ -1,6 +1,5 @@
defmodule Mix.Tasks.Aoc do
use Mix.Task
import Aoc2023.Common
defp module(1), do: Aoc2023.Day1
# [MODULE INSERTION POINT]
@@ -8,18 +7,14 @@ defmodule Mix.Tasks.Aoc do
defp base_dir(), do: System.get_env("AOC_BASE")
defp tests(day) do
case File.ls(tests_dir(day)) do
{:ok, paths} -> paths |> Enum.filter(&(not String.contains?(&1, ".")))
{:error, e} -> dbg(e)
[]
end
File.ls!(tests_dir(day)) |> Enum.filter(&(not String.contains?(&1, ".")))
end
defp read(path) do
f = File.open!(path, [:utf8])
contents = IO.read(f, :eof)
:ok = File.close(f)
String.trim(contents)
contents
end
defp test(day, test) do
@@ -60,15 +55,14 @@ defmodule Mix.Tasks.Aoc do
create_file("#{base_dir()}/lib/day#{day}.ex", """
defmodule Aoc2023.Day#{day} do
use Aoc2023
def parse(input) do
input
end
def part1(input) do
input
end
def part2(input) do
input
end
end
""")
@@ -88,7 +82,7 @@ defmodule Mix.Tasks.Aoc do
end
defp run(day, ["test", "new"]) do
last = tests(day) |> Enum.map(&String.to_integer(&1)) |> Enum.max(&>=/2, fn -> 0 end)
last = tests(day) |> Enum.map(&String.to_integer(&1)) |> Enum.max(empty_fallback: fn -> 1 end)
create_file("#{tests_dir(day)}#{last + 1}", IO.read(:stdio, :eof))
end
@@ -107,7 +101,7 @@ defmodule Mix.Tasks.Aoc do
p1 = mod.part1(parsed)
if p1e do
if stringify(p1) != p1e do
if to_string(p1) != p1e do
IO.puts("Failed at #{test}.1: expected #{p1e}, got:")
dbg(p1)
end
@@ -119,7 +113,7 @@ defmodule Mix.Tasks.Aoc do
p2 = mod.part2(parsed)
if p2e do
if stringify(p2) != p2e do
if to_string(p2) != p2e do
IO.puts("Failed at #{test}.2: expected #{p2e}, got:")
dbg(p2)
end
-4
View File
@@ -1,4 +0,0 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
-1
View File
@@ -1 +0,0 @@
142
-1
View File
@@ -1 +0,0 @@
142
-7
View File
@@ -1,7 +0,0 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
-1
View File
@@ -1 +0,0 @@
209
-1
View File
@@ -1 +0,0 @@
281