groovyda

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

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)
mudasobwa

mudasobwa

Creator of Cure

Access is the extremely powerful and most underrated Elixir feature.

mudasobwa

mudasobwa

Creator of Cure

Finite number of moves is a synonym of reduce/3 through :slight_smile:

adamu

adamu

I thought about Access but I thought, “piles of crates with an index” sounds like a map!

Just throwing mine into the mix.

  defp move_9000(0, from, to), do: {from, to}
  defp move_9000(number, [crate | from], to), do: move_9000(number - 1, from, [crate | to])

  defp move_9001(number, from, to) do
    {moving, moved_from} = Enum.split(from, number)
    {moved_from, moving ++ to}
  end

Completing in <100μs so can’t be too bad :slight_smile:

mudasobwa

mudasobwa

Creator of Cure

I could not disagree more.

Oh, wait, I could.

Access is something I use in each and every project, I even created the library to inject a performant Access implementation into home-baked structs (estructura.) Also anyone who dealt with deeply nested structures at least once, would use it again and again for its simplicity, elegance and uniformness.

Where Next?

Popular in Challenges Top

igorb
Today is a brute-force day: advent-of-code-2024/lib/advent_of_code2024/day6.ex at main · ibarakaiev/advent-of-code-2024 · GitHub Takes a...
New
bjorng
This topic is about Day 18 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
sukhmeetsd
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
bjorng
My solution finishes both parts in 5 seconds on my computer. That time should be possible to reduce by optimizing my rather naive tilt/2 ...
New
stevensonmt
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
gangstead
This is my second year doing AoC in Elixir and my first year doing it with Livebook. When I was doing just plain Elixir I usually set up...
New
QuinnWilton
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
Aetherus
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby. Anyway, here is my solution:
New
DmitriyChernyavskiy
Hello everyone, I’m a new in elexir and functional language. I’m trying to implement Websocket interraction with server. On first layer...
New
bjorng
This topic is about Day 16 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
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 record...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement