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