Maxximiliann
defmodule ArrangeOpeningCombos do
def remove_illegal_third_moves_after_e4(potential_line) do
[one, w1, b1, two, w2, b2, three, w3, b3, four, w4, b4] = potential_line
illegal_moves_map = %{
"Bd3" => [
"a5",
"b5",
"c5",
"d3",
"d4",
"d5",
"e5",
"f5",
"g5",
"h5",
"Nd2",
"Ba3",
"Bb2",
"Bc2",
"Be3",
"Bf4",
"Bf5",
"Bg5",
"Bg6",
"Bh6"
]
}
cond do
w2 == "Bd3" ->
illegal_moves_list = Map.get(illegal_moves_map, "Bd3")
if w3 not in illegal_moves_list do
"(#{one} #{w1} #{b1} #{two} #{w2} #{b2} #{three} #{w3} #{b3} #{four} #{w4} #{b4}) "
end
true ->
"(#{one} #{w1} #{b1} #{two} #{w2} #{b2} #{three} #{w3} #{b3} #{four} #{w4} #{b4}) "
# potential_line
end
end
def remove_illegal_second_moves_after_e4(possible_line) do
[one, w1, b1, two, w2, b2, three, w3, b3, four, w4, b4] = possible_line
list_of_illegal_second_moves_after_e4 = [
"a5",
"b5",
"c5",
"d5",
"e5",
"f5",
"g5",
"h5",
"Nd2",
"Ba3",
"Bb2",
"Bd2",
"Be3",
"Bf4",
"Bg5",
"Bg2",
"Bh3",
"Bh6"
]
if w2 not in list_of_illegal_second_moves_after_e4 do
[one, w1, b1, two, w2, b2, three, w3, b3, four, w4, b4]
end
|> remove_illegal_third_moves_after_e4()
end
def remove_illegal_second_moves_after_d4(possible_line) do
[one, w1, b1, two, w2, b2, three, w3, b3, four, w4, b4] = possible_line
list_of_illegal_second_moves_after_d4 = [
"a5",
"b5",
"c5",
"d5",
"e5",
"f5",
"g5",
"h5",
"Ne2",
"Ba3",
"Ba6",
"Bb2",
"Bb5",
"Bc4",
"Bd3",
"Be2",
"Bg2",
"Bh3"
]
if w2 not in list_of_illegal_second_moves_after_d4 do
"(#{one} #{w1} #{b1} #{two} #{w2} #{b2} #{three} #{w3} #{b3} #{four} #{w4} #{b4}) "
end
# |> remove_illegal_third_moves_after_d4()
end
def scrub_moves_list(moves_list) do
Enum.map(
moves_list,
fn [one, w1, b1, two, w2, b2, three, w3, b3, four, w4, b4] = possible_line ->
cond do
w1 == "e4" ->
remove_illegal_second_moves_after_e4(possible_line)
w1 == "d4" ->
remove_illegal_second_moves_after_d4(possible_line)
true ->
"(#{one} #{w1} #{b1} #{two} #{w2} #{b2} #{three} #{w3} #{b3} #{four} #{w4} #{b4}) "
end
end
)
end
def permute([]), do: [[]]
def permute(list) do
for x <- list, y <- permute(list -- [x]), do: [x | y]
end
def go(opening_moves_string) do
[
one,
white1,
black1,
two,
white2,
black2,
three,
white3,
black3,
four,
white4,
black4
] = String.split(opening_moves_string)
move_numbering = [one, two, three, four]
whites_moves_list = [white1, white2, white3, white4]
blacks_moves_list = [black1, black2, black3, black4]
permutations = permute(whites_moves_list)
combinations_list =
Enum.map(
permutations,
fn moves_list ->
Enum.zip(move_numbering, moves_list)
|> Enum.zip(blacks_moves_list)
|> Enum.flat_map(fn {{number, whites_move}, blacks_move} ->
[number, whites_move, blacks_move]
end)
end
)
Enum.map(
combinations_list,
fn [
one,
w1,
b1,
two,
w2,
b2,
three,
w3,
b3,
four,
w4,
b4
] ->
illegal_first_moves = [
"a5",
"b5",
"c5",
"d5",
"e5",
"f5",
"g5",
"h5",
"Ne2",
"Nd2",
"Ba3",
"Ba6",
"Bb2",
"Bb5",
"Bc4",
"Bd3",
"Be2",
"Bd2",
"Be3",
"Bf4",
"Bg5",
"Bg2",
"Bh3",
"Bh6"
]
if w1 not in illegal_first_moves do
[one, w1, b1, two, w2, b2, three, w3, b3, four, w4, b4]
end
end
)
|> Enum.reject(&is_nil(&1))
|> scrub_moves_list()
|> Enum.reject(&is_nil(&1))
|> IO.puts()
end
end
Output:
Interactive Elixir (1.10.3) - press Ctrl+C to exit (type h() ENTER for help)
Elixir is ready
iex(1)> ArrangeOpeningCombos.go("1. e4 e6 2. d4 Ke7 3. Bd3 f6 4. Ne2 Kf7")
(1. e4 e6 2. d4 Ke7 3. Bd3 f6 4. Ne2 Kf7) (1. e4 e6 2. d4 Ke7 3. Ne2 f6 4. Bd3 Kf7) (1. e4 e6 2. Bd3 Ke7 3. Ne2 f6 4. d4 Kf7) (1. e4 e6 2. Ne2 Ke7 3. d4 f6 4. Bd3 Kf7) (1. e4 e6 2. Ne2 Ke7 3. Bd3 f6 4. d4 Kf7) (1. d4 e6 2. e4 Ke7 3. Bd3 f6 4. Ne2 Kf7) (1. d4 e6 2. e4 Ke7 3. Ne2 f6 4. Bd3 Kf7)
:ok
Ok, so, I’ve been learning Elixir for about a year now despite having zero programming experience - thanks so much to everyone on here who’s so kindly and generously helped me along the way
- and I’d like to get a sense of where I’m at, what I’m doing correctly, what bad habits I’ve picked up along the way, if any, and how I can write code that is more efficient, readable and idiomatic while conforming to best practices. To that end I’ve put together this toy project.
Any and all tips, suggestions, recommendations (and encouragements
) are warmly welcomed. Thanks again guys! ![]()
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
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
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
With
Elixirformatter this is better:as that your list would not be expanded in so many lines …
Shorter:
Optionally:
You may use pattern matching for this like:
Much better:
scrub_moves_list/1always returns list, so secondEnum.reject/2is not needed.Also you can write
&is_nil/1.Finally having
"(" <> Enum.join(possible_line, " ") <> ") "multiple times we may want to create a helper function for that.Maxximiliann
Great stuff, thanks so very, very much!