Skip to content

Commit

Permalink
Complete Day 1 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
duff committed Nov 22, 2024
1 parent 7bbfa22 commit dea9344
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
57 changes: 49 additions & 8 deletions lib/advent_2023/day_01.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
defmodule Advent2023.Day01 do
def calibration_value(input) do
digit_string = digits(input)
input
|> digits_only()
|> first_last()
end

"#{String.first(digit_string)}#{String.last(digit_string)}"
|> String.to_integer()
def calibration_value_with_words(input) do
input
|> digits_with_words()
|> first_last()
end

def calibration_sum(lines) do
Expand All @@ -13,10 +18,46 @@ defmodule Advent2023.Day01 do
|> Enum.sum()
end

defp digits(str) do
str
|> String.to_charlist()
|> Enum.filter(fn x -> x in ?0..?9 end)
|> List.to_string()
def calibration_sum_with_words(lines) do
lines
|> String.split("\n", trim: true)
|> Enum.map(&calibration_value_with_words/1)
|> Enum.sum()
end

defp digits_only(str) do
Regex.scan(~r{[1-9]}, str)
|> List.flatten()
end

defp digits_with_words(str) do
Regex.scan(~r{\d|oneight|twone|eightwo|nineight|eighthree|one|two|three|four|five|six|seven|eight|nine}, str)
|> Enum.map(&to_digits/1)
|> List.flatten()
end

defp first_last(list) do
"#{List.first(list)}#{List.last(list)}"
|> String.to_integer()
end

defp to_digits([str]) do
case str do
"oneight" -> [1, 8]
"twone" -> [2, 1]
"eightwo" -> [8, 2]
"nineight" -> [9, 8]
"eighthree" -> [8, 3]
"one" -> 1
"two" -> 2
"three" -> 3
"four" -> 4
"five" -> 5
"six" -> 6
"seven" -> 7
"eight" -> 8
"nine" -> 9
_ -> String.to_integer(str)
end
end
end
34 changes: 34 additions & 0 deletions test/lib/advent_2023/day_01_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ defmodule Advent2023.Day01Test do
assert Day01.calibration_sum(input) == 55_834
end

test "calibration_value_with_words" do
assert Day01.calibration_value_with_words("pqr3stu8vwx") == 38
assert Day01.calibration_value_with_words("4nineeightseven2") == 42
assert Day01.calibration_value_with_words("xtwone3four") == 24
assert Day01.calibration_value_with_words("7pqrstsixteen") == 76
assert Day01.calibration_value_with_words("6huhoneight") == 68
assert Day01.calibration_value_with_words("twone") == 21
assert Day01.calibration_value_with_words("eightwo") == 82
assert Day01.calibration_value_with_words("nineight") == 98
assert Day01.calibration_value_with_words("eighthree") == 83
end

test "calibration_sum_with_words" do
assert Day01.calibration_sum_with_words(input_words()) == 281
assert Day01.calibration_sum_with_words(input()) == 142
end

test "calibration_sum_with_words real" do
input = File.read!("test/lib/advent_2023/input/day_01.txt")
assert Day01.calibration_sum_with_words(input) == 53_221
end

defp input do
"""
1abc2
Expand All @@ -27,4 +49,16 @@ defmodule Advent2023.Day01Test do
treb7uchet
"""
end

defp input_words do
"""
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
"""
end
end

0 comments on commit dea9344

Please sign in to comment.