Day 01
Mix.install([
{:kino, "~> 0.7.0"}
])
IEx.Helpers.c("/Users/johnb/dev/2023adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input
Installation and Data
input_day = Kino.Input.number("Day")
input_p1example = Kino.Input.textarea("Example Data")
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input")
input_source_select =
Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
p1data = fn ->
(Kino.Input.read(input_source_select) == :example &&
Kino.Input.read(input_p1example)) ||
Kino.Input.read(input_p1puzzleInput)
end
Part 1
Day 1: Trebuchet?!
defmodule Day01 do
def only_digits_as_integer(line) do
str = String.replace(line, ~r/[^\d]/, "", global: true)
(String.at(str, 0) <> String.at(str, -1))
|> String.to_integer()
end
def solve1(text) do
text
|> AOC.as_single_lines()
|> Enum.map(&only_digits_as_integer/1)
|> Enum.sum()
end
def part_two_to_integer(line) do
str =
line
|> String.replace(~r/zero/i, "z0ero")
|> String.replace(~r/one/i, "o1ne")
|> String.replace(~r/two/i, "t2wo")
|> String.replace(~r/three/i, "t3hree")
|> String.replace(~r/four/i, "f4our")
|> String.replace(~r/five/i, "f5ive")
|> String.replace(~r/six/i, "s6ix")
|> String.replace(~r/seven/i, "s7even")
|> String.replace(~r/eight/i, "e8ight")
|> String.replace(~r/nine/i, "n9ine")
|> String.replace(~r/[^\d]/, "", global: true)
(String.at(str, 0) <> String.at(str, -1))
|> String.to_integer()
end
def solve2(text) do
text
|> AOC.as_single_lines()
# |> IO.inspect()
|> Enum.map(&part_two_to_integer/1)
|> Enum.sum()
# |> IO.inspect()
end
end
# 1abc2
# pqr3stu8vwx
# a1b2c3d4e5f
# treb7uchet
# p1data.()
# |> Day01.solve1()
# 142
# 54159
p1data.()
|> Day01.solve2()
# 281
# 53866