Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 1

advent_of_code/2023/day-01.livemd

Day 1

Day 1: Trebuchet?!

Day 1: Trebuchet?!

Input

input = """
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
"""

Part 1

f = fn s -> s |> String.to_charlist() |> Enum.filter(fn c -> c in ?0..?9 end) end
f2 = fn list -> "#{Enum.at(list, 0) - ?0}#{Enum.at(list, -1) - ?0}" |> String.to_integer() end
input |> String.split("\n", trim: true) |> Enum.map(&(f.(&1) |> f2.())) |> Enum.sum()

Part 2

input = """
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
"""
f = fn s ->
  s
  |> String.replace(
    ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"],
    fn
      "one" -> "1e"
      "two" -> "2o"
      "three" -> "3e"
      "four" -> "4r"
      "five" -> "5e"
      "six" -> "6x"
      "seven" -> "7n"
      "eight" -> "8t"
      "nine" -> "9e"
    end
  )
  |> String.replace(
    ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"],
    fn
      "one" -> "1e"
      "two" -> "2o"
      "three" -> "3e"
      "four" -> "4r"
      "five" -> "5e"
      "six" -> "6x"
      "seven" -> "7n"
      "eight" -> "8t"
      "nine" -> "9e"
    end
  )
  |> String.to_charlist()
  |> Enum.filter(fn c -> c in ?0..?9 end)
end

f2 = fn list -> "#{Enum.at(list, 0) - ?0}#{Enum.at(list, -1) - ?0}" |> String.to_integer() end

input |> String.split("\n", trim: true) |> Enum.map(&(f.(&1) |> f2.())) |> Enum.sum()