dkuku

dkuku

Solved- Nested map - how to access value from previous iteration?

I’m trying to solve aoc day 3 and my idea is to generate list of points
[“R2”,“U3”]
starting from the point {0,0} then 2 points Right and 3 points Up
the results needs to be:

[
    [{1,0},{2,0}],
    [{2,1},{2,2}{2,3}]
]

or as a drawing:

..u..
..u..
..u..
orr..

my implementation is that I map over the first list,
inside I pattern match on the direction and create a range with the length of vector then again I map over the range to generate the points.
The problem is that every nested map needs a different starting point (the last point from previous list) and I’m not really sure how to pass this variable in elixir - I’m thinking about a global variable but there is probably an easier solution

PS. I don’t want to ask in the aoc thread due to spoilers :wink: and I think the problem is more generic

Solution: the longer I think about it I need Enum.reduce instead of map and accumulate the new list in accumulator - this way I will have access to it in the next iteration

Marked As Solved

dkuku

dkuku

I don’t think that I need the whole grid for this particular solution.
I just need to generate list of all the points contained in the vectors.

That’s why I think the question is more generic and if someone has similar problem he won’t probably search in an aoc thread :wink:

The drawing is just for explaining what I want to achieve.
Solution: the longer I think about it I need Enum.reduce instead of map and accumulate the new list in accumulator - this way I will have access to it in the next iteration

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

We're in Beta

About us Mission Statement