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:

Where Next?

Popular in Challenges Top

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
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
Here is my solution: https://github.com/bjorng/advent-of-code-2021/blob/77bdef7a51b428b58ffb4ab76f540901e636f841/day12/lib/day12.ex
New
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
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
liamcmitchell
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
connorlay
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
bjorng
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
bjorng
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
christhekeele
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 Top

danschultzer
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...
548 29377 241
New
hariharasudhan94
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
jason.o
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
saif
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
dblack
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
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 records...
New
lanycrost
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

We're in Beta

About us Mission Statement