groovyda
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
Trending in Challenges
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
code-shoily
I spent more time than I would have only if I hadn’t updated the “initial state” instead of “accumulator” inside that reducer. It was silly of me and thanks to
dbg, copying the example data, and 10 minutes of exploration, I got back on track. Cute of me to think I could get away with a_in lieu ofacclol.Here’s the outcome: advent_of_code/lib/2022/day_05.ex at main · code-shoily/advent_of_code · GitHub
stevensonmt
Biggest challenge for me was just parsing the input, which is pretty typical for me.
I probably went overboard spinning up a GenServer but it’s what I always think of when there’s some “moves” or “ops” that have to be tracked.
mudasobwa
As always, I am here to advertise
Access.groovyda
Thanks. Learnt something new!
mudasobwa
Accessis the extremely powerful and most underrated Elixir feature.mudasobwa
Finite number of moves is a synonym of
reduce/3throughstevensonmt
Sure. Wasn’t sure what part 2 was going to be though, so it felt most flexible to do it with a GenServer.
lkuty
I think the use of a module and named functions greatly improves readability. But I wanted to be as close as possible to one-liners.
part1
Part 2 is very similar to part 1. The way of moving multiple crates is simplified since a
reducedisappears.part2
kwando
I put each stack of crates in a map, indexed by the stacks index so I could access them without traversing the list of stacks all the time
Thanks for pointing me to the
Accessmodule, there are indeed some neat stuff in there that I didn’t know about.bmitc
I implemented my own
Stackmodule that just wrapsListwith some (hopefully) more stack-ish names. I haven’t decided if this was overkill or not, but I thought it was okay in the end. My solution is just converting the move instructions to stack operations (peek, drop, and push). I didn’t see a cleaner way of doing a pop and not peek and drop, This one might be one I revisit with fresh eyes and consider a major refactor if I think of a cleaner way after reviewing solutions here tomorrow. advent-of-code/2022/elixir/advent_of_code_2022.livemd at main · bmitc/advent-of-code · GitHubAlso, I hand-coded the initial stacks because I didn’t want to waste time decoding such terrible input.
(Gotta scroll to see the bulk of the solution.)