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

Aetherus
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
bjorng
Note: This topic is to talk about Day 5 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
groovyda
Today’s challenge for me was about using reduce: defmodule Prob5 do def move([[h1 | rest] = _list1, list2]) do [rest, [h1 | list2]...
New
bjorng
This topic is about Day 9 of the Advent of Code 2021 . We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
Aetherus
This topic is about Day 4 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
Aetherus
This topic is about Day 7 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
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
bjorng
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
bjorng
This topic is about Day 6 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
rvnash
Anyone have a solution to Part 2 today? Part 1 was straight forward, but I can’t figure out a programatic way to do part 2. I understand ...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement