bjorng

bjorng

Erlang Core Team

Advent of Code 2025 - Day 6

defmodule Day06 do
  def part1(input) do
    input
    |> Enum.map(fn line ->
      line
      |> String.split(" ", trim: true)
      |> Enum.map(fn token ->
        case Integer.parse(token) do
          :error -> String.to_atom(token)
          {n, ""} -> n
        end
      end)
    end)
    |> transpose
    |> Enum.map(fn column ->
      operator = List.last(column)
      Enum.drop(column, -1)
      |> Enum.reduce(&(eval(operator, &1, &2)))
    end)
    |> Enum.sum
  end

  def part2(input) do
    operators = List.last(input)
    spaces = count_spaces(String.to_charlist(operators))
    operators = operators
    |> String.to_charlist
    |> Enum.filter(&(&1 !== ?\s))
    |> Enum.map(&List.to_atom([&1]))

    input
    |> Enum.drop(-1)
    |> Enum.map(fn line ->
      String.to_charlist(line)
      |> split_line(spaces)
    end)
    |> transpose
    |> Enum.zip(operators)
    |> Enum.map(fn {column, operator} ->
      column
      |> transpose
      |> Enum.map(fn column ->
        column
        |> Enum.filter(&(&1 !== ?\s))
        |> List.to_integer
      end)
      |> Enum.reduce(&(eval(operator, &1, &2)))
    end)
    |> Enum.sum
  end

  defp split_line([], []), do: []
  defp split_line(line, [n]) do
    {number, []} = Enum.split(line, n + 1)
    [number]
  end
  defp split_line(line, [n | ns]) do
    {number, rest} = Enum.split(line, n)
    [?\s | rest] = rest
    [number | split_line(rest, ns)]
  end

  defp count_spaces([]), do: []
  defp count_spaces([_op|rest]) do
    {spaces, rest} = Enum.split_while(rest, &(&1 === ?\s))
    [length(spaces) | count_spaces(rest)]
  end

  defp eval(operator, number1, number2) do
    apply(Kernel, operator, [number1, number2])
  end

  defp transpose(list) do
    Enum.zip_with(list, &Function.identity/1)
  end
end

Most Liked

zwippie

zwippie

That one is going straight into my Aoc.Utils, thank you!

hauleth

hauleth

Setup

{tasks, [ops]} =
  puzzle_input
  |> String.split("\n", trim: true)
  |> Enum.split(-1)

ops =
  ops
  |> String.split()
  |> Enum.map(&String.to_atom/1)

Part 1

tasks
|> Enum.map(&String.split/1)
|> Enum.zip_with(fn numbers -> Enum.map(numbers, &String.to_integer/1) end)
|> Enum.zip(ops)
|> Enum.sum_by(fn
  {nums, :+} -> Enum.sum(nums)
  {nums, :*} -> Enum.product(nums)
end)

Part 2

tasks
|> Enum.map(&String.to_charlist/1)
|> Enum.zip_with(&(&1 |> List.to_string() |> String.trim()))
|> Enum.chunk_while(
  [],
  fn
    "", acc -> {:cont, acc, []}
    num, acc -> {:cont, [String.to_integer(num) | acc]}
  end,
  &{:cont, &1, []}
)
|> Enum.zip(ops)
|> Enum.sum_by(fn
  {nums, :+} -> Enum.sum(nums)
  {nums, :*} -> Enum.product(nums)
end)
hauleth

hauleth

Enum.product(list)

Where Next?

Popular in Challenges Top

bjorng
Here is my solution for day 2 of Advent of Code: https://github.com/bjorng/advent-of-code/blob/main/2024/day02/lib/day02.ex
New
kwando
Took a while, but another use case for “move vectors” today and pattern matching. :slight_smile: The trick was to first generate a list...
New
kwando
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
antoine-duchenet
Everything went smoothly today. Nothing to change to solve part 2 because I already used memoization for part 1 (it looked like an AoC e...
New
New
bjorng
Note: This topic is to talk about Day 3 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
This topic is about Day 1 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
Aetherus
I tried to use combinatorial to solve today’s puzzles but failed (my brain burned out :exploding_head:). In the end I just used brute for...
New
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
lud
Hello everyone! This year is going to be shorter, but the difficulty will grow faster. Today I already feel that this is not standard “D...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement