blackened
I am a complete Elixir beginner. I am trying to understand certain concepts with the following Mastermind sub-problem.
Suppose I have two lists code = [0, 3, 2 ,3, 4, 5] and guess = [0, 4, 2, 4, 3, 3].
My approach is, we compare the two lists and assign two values, one for both correct guess and position (CP), and the other for correct guess but for wrong position (CG). In the above example: CP is 2 (the first and third guess), and the CG is 3 (two 3s and one 4).
I guess there are numerous ways to approach. What I wanted to achieve was this: We traverse the two lists to determine correct positional guesses and produce two new lists. For the above example, that would be:
[:true, 3, :true, 3, 4, 5] and [:true, 4, :true, 4, 3, 3]
Then, we traverse the two lists for the second time and produce a third list, such that we take each item from the new guess list and check if the code list contains it. If so, the new list returns, say, :guess for those guesses. So the third list will be:
[:true, :guess, :true, :guess, :guess: 5] and [:true, :ok, :true, 4, :ok, :ok]
And now we count the :true, and :guess.
- What is the Elixir way to produce the second list and the third list.
- How else can I approach it anyway (again, in an idiomatic Elixir way)?
Trending in Questions
Other Trending Topics
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 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Sebb
Nice problem to learn some Enum functions.
The ‘CP’ problem can easily be solved with
zip‘CG’ is a little more involved, there are several functions in Enum one could use. There is most likely some better approach, but for example this seems to work:
Have a look at the other functions in
Enum. Try to solve 'CG" without the help offrequencies.This may help: Elixir Enum Cheatsheet
alfredfriedrich
My surely not so Elixir way approach:
Will produce something like:
Thanks for the opportunity to read up again about
Enumandmap_reducematt-savvy
I love these games. I wrote a mastermind game & solver a few years ago in JS and the evaluation fn
relied heavily on mutating arrays. (also recently wrote a wordle knockoff & solver in Elm that used an approach similar to the
Enum.frequenciesapproach above).Here’s my crack at the evaluation in Elixir, but I’m also still new to the language.
I really like Sebb’s approach with
Enum.split_with, otherwise I would have just done one round of mapping the correct positions to an atom, another round of reducing to come up with atoms for the correct guesses, and then rejected everything from the list that wasn’t an atom (just usingKernel.is_atom/1)matt-savvy
@blackened
I just had a thought and I’m pretty sure you can do this to get the remaining correct guesses (as in, right number, wrong place)