groovyda
Advent of Code 2022 - Day 5
Today’s challenge for me was about using reduce:
defmodule Prob5 do
def move([[h1 | rest] = _list1, list2]) do
[rest, [h1 | list2]]
end
def move_n([l1, l2], n) do
Enum.reduce(1..n, [l1, l2], fn _, acc -> move(acc) end)
end
def single_instruction(input, [n, from_ind, to_ind], move_function) do
from_ind = from_ind - 1
to_ind = to_ind - 1
[from, to] = move_function.([Enum.at(input, from_ind), Enum.at(input, to_ind)], n)
input
|> List.replace_at(from_ind, from)
|> List.replace_at(to_ind, to)
end
def parse_line(line) do
[[n], [from_ind], [to_ind]] = Regex.scan(~r/\d+/, line)
Enum.map([n, from_ind, to_ind], &String.to_integer/1)
end
def move_n_part2([l1, l2], n) do
to_move = Enum.take(l1, n)
[Enum.drop(l1, n), to_move ++ l2]
end
end
I manually parsed the first part of the input into lists, like thus:
test_input = [[:n, :z], [:d, :c, :m], [:p]]
And then parsed the 2nd part of the input into simple lists, and finally ran it through reduce:
Enum.reduce(
instructions,
input,
fn instruction, input -> Prob5.single_instruction(input, instruction, &Prob5.move_n/2) end)
… using the 2 move functions - one for each part.
Let me know how this can be improved!
Thanks
Most Liked
mudasobwa
Creator of Cure
As always, I am here to advertise Access.
# moving crates (part I)
Enum.reduce(0..count - 1, acc, fn _, acc ->
{v, acc} = get_and_update_in(acc, [Access.at(from - 1)], fn [h|t] -> {h, t} end)
update_in(acc, [Access.at(to - 1)], fn t -> [v|t] end)
end)
# moving crates (part II)
{v, acc} = get_and_update_in(acc, [Access.at(from - 1)], fn l -> Enum.split(l, count) end)
update_in(acc, [Access.at(to - 1)], fn t -> v ++ t end)
7
mudasobwa
Creator of Cure
mudasobwa
Creator of Cure
Finite number of moves is a synonym of reduce/3 through ![]()
3
Popular in Challenges
Note: This topic is to talk about Day 7 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can joi...
New
This topic is about Day 15 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Here is my solution:
https://github.com/bjorng/advent-of-code-2021/blob/77bdef7a51b428b58ffb4ab76f540901e636f841/day12/lib/day12.ex
New
This topic is about Day 5 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Here is my solution for day 1 of Advent of Code:
defmodule Day01 do
def part1(input) do
all = parse(input)
{first, second} = E...
New
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018.
To prevent people from being spoiled about s...
New
Note: This topic is to talk about Day 6 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Note: This topic is to talk about Day 23 of the Advent of Code.
For general discussion about the Advent of Code 2018 and links to topics...
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
Other popular topics
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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








