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
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)
3
hauleth
Popular in Challenges
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
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
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
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
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
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
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
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
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
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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...
New
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
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










