Day 1
This commit is contained in:
42
lib/day1.ex
42
lib/day1.ex
@@ -2,14 +2,48 @@ defmodule Aoc2023.Day1 do
|
||||
use Aoc2023
|
||||
|
||||
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
|
||||
|
||||
def part1(input) do
|
||||
nil
|
||||
input.(fn line -> line end)
|
||||
end
|
||||
|
||||
def part2(input) do
|
||||
nil
|
||||
@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)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user