Advent of Code 2023 - Day 6

Greetings, everyone! Ukrainian Erlanger is here to check in :metal:! I hope you’re all enjoying the thrill of puzzle-solving. As we find ourselves in the midst of the week, it’s time for Day 6 of Advent of Code! A new puzzle challenge awaits, so don’t miss out on this opportunity. Embrace the challenge and, of course, happy coding :blush:!

2 Likes

Here is my day 6 solution:

defmodule Aoc.Six do
  def parse(string) do
    string
    |> String.split("\n", trim: true)
    |> Enum.map(fn line ->
      line
      |> String.split(" ", trim: true)
      |> tl()
      |> Enum.map(&String.to_integer/1)
    end)
  end

  def part_one(input) do
    input
    |> Enum.zip()
    |> Enum.map(&math/1)
    |> Enum.product()
  end

  def part_two(input) do
    input
    |> Enum.map(fn line ->
      line |> Enum.map(&Integer.to_string/1) |> Enum.join() |> String.to_integer()
    end)
    |> List.to_tuple()
    |> math()
  end

  def math({time, distance}) do
    sqrt = :math.sqrt(time ** 2 - 4 * distance)
    a = (time - sqrt) / 2
    b = (time + sqrt) / 2
    ceil(a)..floor(b) |> Enum.count()
  end
end

input = File.read!("priv/6.txt") |> Aoc.Six.parse()

input |> Aoc.Six.part_one() |> IO.inspect(label: "part 1")
input |> Aoc.Six.part_two() |> IO.inspect(label: "part 2")
1 Like