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

Day 07

day07.livemd

Day 07

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

IEx.Helpers.c("/Users/johnb/dev/2024adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input

# Note: when making the next template, something like this works well:
#   `cat day04.livemd | sed 's/07/04/' > day04.livemd`
#
# When inspecting lists of numbers, use "charlists: :as_lists"

Installation and Data

input_p1example = Kino.Input.textarea("Example Data", monospace: true)
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input", monospace: true)
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

Solution

defmodule Day07 do
  def parse(text) do
    text
    |> AOC.as_single_lines()
    |> Enum.map(fn line ->
      [result, numbers] = String.split(line, ":", trim: true)
      numbers = String.split(numbers, ~r/\W/, trim: true)
        |> Enum.map(&String.to_integer/1)
      {String.to_integer(result), numbers}
    end)
  end

  def can_find_equality?(result, [first]), do: result == first
  def can_find_equality?(result, [first, second | rest]) do
    can_find_equality?(result, [(first + second) | rest]) or
      can_find_equality?(result, [(first * second) | rest])
  end

  def can_find_3way_equality?(result, [first]), do: result == first
  def can_find_3way_equality?(result, [first, second | rest]) do
    can_find_3way_equality?(result, [(first + second) | rest]) or
      can_find_3way_equality?(result, [(first * second) | rest]) or 
      can_find_3way_equality?(result, [String.to_integer("#{first}#{second}") | rest])
  end

  def solve1(text) do
    parse(text)
    |> Enum.filter(fn {result, numbers} ->
      can_find_equality?(result, numbers)
    end)
    |> Enum.reduce(0, fn {result, _number}, acc -> acc + result end)
  end

  def solve2(text) do
    parse(text)
    |> Enum.filter(fn {result, numbers} ->
      can_find_3way_equality?(result, numbers)
    end)
    |> Enum.reduce(0, fn {result, _number}, acc -> acc + result end)
  end
end

# Example:

p1data.()
|> Day07.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 3749)")
# 850435817339

p1data.()
|> Day07.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 11387)")
# 104824810233437