Compare commits

..

2 Commits

Author SHA1 Message Date
bluepython508 cf508c9396 Day 1 2023-12-01 19:13:18 +00:00
bluepython508 235deeccb7 Infra 2023-12-01 19:13:00 +00:00
9 changed files with 68 additions and 11 deletions
+2
View File
@@ -1,2 +1,4 @@
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
+38 -4
View File
@@ -2,14 +2,48 @@ defmodule Aoc2023.Day1 do
use Aoc2023 use Aoc2023
def parse(input) do def parse(input) do
input 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 end
def part1(input) do def part1(input) do
nil input.(fn line -> line end)
end end
def part2(input) do @numbers [
nil "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 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)
end
end end
+13 -7
View File
@@ -1,5 +1,6 @@
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]
@@ -7,14 +8,18 @@ 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
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 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)
contents String.trim(contents)
end end
defp test(day, test) do defp test(day, test) do
@@ -55,14 +60,15 @@ 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
""") """)
@@ -82,7 +88,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(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)) create_file("#{tests_dir(day)}#{last + 1}", IO.read(:stdio, :eof))
end end
@@ -101,7 +107,7 @@ defmodule Mix.Tasks.Aoc do
p1 = mod.part1(parsed) p1 = mod.part1(parsed)
if p1e do if p1e do
if to_string(p1) != p1e do if stringify(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
@@ -113,7 +119,7 @@ defmodule Mix.Tasks.Aoc do
p2 = mod.part2(parsed) p2 = mod.part2(parsed)
if p2e do if p2e do
if to_string(p2) != p2e do if stringify(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
+4
View File
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
+1
View File
@@ -0,0 +1 @@
142
+1
View File
@@ -0,0 +1 @@
142
+7
View File
@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
+1
View File
@@ -0,0 +1 @@
209
+1
View File
@@ -0,0 +1 @@
281