Day 1
This commit is contained in:
38
lib/day1.ex
38
lib/day1.ex
@@ -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
|
||||
|
||||
4
tests/day1/1
Normal file
4
tests/day1/1
Normal file
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
1
tests/day1/1.1
Normal file
1
tests/day1/1.1
Normal file
@@ -0,0 +1 @@
|
||||
142
|
||||
1
tests/day1/1.2
Normal file
1
tests/day1/1.2
Normal file
@@ -0,0 +1 @@
|
||||
142
|
||||
7
tests/day1/2
Normal file
7
tests/day1/2
Normal file
@@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
1
tests/day1/2.1
Normal file
1
tests/day1/2.1
Normal file
@@ -0,0 +1 @@
|
||||
209
|
||||
1
tests/day1/2.2
Normal file
1
tests/day1/2.2
Normal file
@@ -0,0 +1 @@
|
||||
281
|
||||
Reference in New Issue
Block a user