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

Advent of Code 2023 - Day 1

advent-of-code/2023/day01.livemd

Advent of Code 2023 - Day 1

Mix.install([
  {:kino, "~> 0.11.3"}
])

Puzzle Input

puzzle_input_kino = Kino.Input.textarea("Please paste the puzzle input:")
puzzle_input = Kino.Input.read(puzzle_input_kino)

Part 1

part_1_test_input = """
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
"""
part_1_test_input
|> String.split("\n", trim: true)
|> Enum.map(fn string ->
  digits = String.replace(string, ~r/\D/, "")
  first = String.first(digits)
  last = String.last(digits)
  "#{first}#{last}"
end)
|> Enum.map(&String.to_integer/1)
|> Enum.sum()
puzzle_input
|> String.split("\n", trim: true)
|> Enum.map(fn string ->
  digits = String.replace(string, ~r/\D/, "")
  first = String.first(digits)
  last = String.last(digits)
  "#{first}#{last}"
end)
|> Enum.map(&String.to_integer/1)
|> Enum.sum()

Part 2

part_2_test_input = """
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
"""
part_2_test_input
|> String.split("\n", trim: true)
|> Enum.map(fn string ->
  string
  |> String.replace("one", "o1e")
  |> String.replace("two", "t2")
  |> String.replace("three", "t3e")
  |> String.replace("four", "4")
  |> String.replace("five", "5")
  |> String.replace("six", "6s")
  |> String.replace("seven", "7n")
  |> String.replace("eight", "e8t")
  |> String.replace("nine", "n9e")
end)
|> Enum.map(fn string ->
  digits = String.replace(string, ~r/\D/, "")
  first = String.first(digits)
  last = String.last(digits)
  "#{first}#{last}"
end)
|> Enum.map(&String.to_integer/1)
|> Enum.sum()
puzzle_input
|> String.split("\n", trim: true)
|> Enum.map(fn string ->
  string
  |> String.replace("one", "o1e")
  |> String.replace("two", "t2")
  |> String.replace("three", "t3e")
  |> String.replace("four", "4")
  |> String.replace("five", "5")
  |> String.replace("six", "6s")
  |> String.replace("seven", "7n")
  |> String.replace("eight", "e8t")
  |> String.replace("nine", "n9e")
end)
|> Enum.map(fn string ->
  digits = String.replace(string, ~r/\D/, "")
  first = String.first(digits)
  last = String.last(digits)
  "#{first}#{last}"
end)
|> Enum.map(&String.to_integer/1)
|> Enum.sum()