Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf508c9396 | |||
| 235deeccb7 |
@@ -1,2 +1,4 @@
|
||||
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
|
||||
|
||||
+36
-2
@@ -2,14 +2,48 @@ 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
|
||||
nil
|
||||
input.(fn line -> line end)
|
||||
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
|
||||
nil
|
||||
input.(&replacements_rec/1)
|
||||
end
|
||||
end
|
||||
|
||||
+13
-7
@@ -1,5 +1,6 @@
|
||||
defmodule Mix.Tasks.Aoc do
|
||||
use Mix.Task
|
||||
import Aoc2023.Common
|
||||
|
||||
defp module(1), do: Aoc2023.Day1
|
||||
# [MODULE INSERTION POINT]
|
||||
@@ -7,14 +8,18 @@ defmodule Mix.Tasks.Aoc do
|
||||
defp base_dir(), do: System.get_env("AOC_BASE")
|
||||
|
||||
defp tests(day) do
|
||||
File.ls!(tests_dir(day)) |> Enum.filter(&(not String.contains?(&1, ".")))
|
||||
case File.ls(tests_dir(day)) do
|
||||
{:ok, paths} -> paths |> Enum.filter(&(not String.contains?(&1, ".")))
|
||||
{:error, e} -> dbg(e)
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp read(path) do
|
||||
f = File.open!(path, [:utf8])
|
||||
contents = IO.read(f, :eof)
|
||||
:ok = File.close(f)
|
||||
contents
|
||||
String.trim(contents)
|
||||
end
|
||||
|
||||
defp test(day, test) do
|
||||
@@ -55,14 +60,15 @@ 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
|
||||
""")
|
||||
@@ -82,7 +88,7 @@ defmodule Mix.Tasks.Aoc do
|
||||
end
|
||||
|
||||
defp run(day, ["test", "new"]) do
|
||||
last = tests(day) |> Enum.map(&String.to_integer(&1)) |> Enum.max(empty_fallback: fn -> 1 end)
|
||||
last = tests(day) |> Enum.map(&String.to_integer(&1)) |> Enum.max(&>=/2, fn -> 0 end)
|
||||
create_file("#{tests_dir(day)}#{last + 1}", IO.read(:stdio, :eof))
|
||||
end
|
||||
|
||||
@@ -101,7 +107,7 @@ defmodule Mix.Tasks.Aoc do
|
||||
p1 = mod.part1(parsed)
|
||||
|
||||
if p1e do
|
||||
if to_string(p1) != p1e do
|
||||
if stringify(p1) != p1e do
|
||||
IO.puts("Failed at #{test}.1: expected #{p1e}, got:")
|
||||
dbg(p1)
|
||||
end
|
||||
@@ -113,7 +119,7 @@ defmodule Mix.Tasks.Aoc do
|
||||
p2 = mod.part2(parsed)
|
||||
|
||||
if p2e do
|
||||
if to_string(p2) != p2e do
|
||||
if stringify(p2) != p2e do
|
||||
IO.puts("Failed at #{test}.2: expected #{p2e}, got:")
|
||||
dbg(p2)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
@@ -0,0 +1 @@
|
||||
142
|
||||
@@ -0,0 +1 @@
|
||||
142
|
||||
@@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
@@ -0,0 +1 @@
|
||||
209
|
||||
@@ -0,0 +1 @@
|
||||
281
|
||||
Reference in New Issue
Block a user