This commit is contained in:
bluepython508
2023-12-01 19:13:18 +00:00
parent 235deeccb7
commit cf508c9396
7 changed files with 53 additions and 4 deletions

View File

@@ -2,14 +2,48 @@ 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
nil input.(fn line -> line end)
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
nil input.(&replacements_rec/1)
end end
end end

4
tests/day1/1 Normal file
View File

@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

1
tests/day1/1.1 Normal file
View File

@@ -0,0 +1 @@
142

1
tests/day1/1.2 Normal file
View File

@@ -0,0 +1 @@
142

7
tests/day1/2 Normal file
View File

@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

1
tests/day1/2.1 Normal file
View File

@@ -0,0 +1 @@
209

1
tests/day1/2.2 Normal file
View File

@@ -0,0 +1 @@
281