Created
February 8, 2018 05:37
-
-
Save elbow-jason/987470da69f2d3e9d9dd2de59e768a8a to your computer and use it in GitHub Desktop.
Black Jack Thing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(11)> :timer.tc(fn -> Outcomes.run end) | |
Dealer showing 0 partitions = 417334 | |
Dealer showing 1 partitions = 560954 | |
Dealer showing 2 partitions = 658854 | |
Dealer showing 3 partitions = 679464 | |
Dealer showing 4 partitions = 680299 | |
Dealer showing 5 partitions = 680305 | |
Dealer showing 6 partitions = 680305 | |
Dealer showing 7 partitions = 680305 | |
Dealer showing 8 partitions = 680305 | |
Dealer showing 9 partitions = 680305 | |
Total partitions = 6398430 | |
{2624312, 6398430} | |
iex(12)> :timer.tc(fn -> CardDeck.run end) | |
Dealer showing 0 partitions = 417334 | |
Dealer showing 1 partitions = 560954 | |
Dealer showing 2 partitions = 658854 | |
Dealer showing 3 partitions = 679464 | |
Dealer showing 4 partitions = 680299 | |
Dealer showing 5 partitions = 680305 | |
Dealer showing 6 partitions = 680305 | |
Dealer showing 7 partitions = 680305 | |
Dealer showing 8 partitions = 680305 | |
Dealer showing 9 partitions = 680305 | |
Total partitions = 6398430 | |
{2646377, 6398430} | |
iex(13)> :timer.tc(fn -> CardDeck.run_parallel end) | |
Dealer showing 0 partitions = 417334 | |
Dealer showing 1 partitions = 560954 | |
Dealer showing 9 partitions = 680305 | |
Dealer showing 5 partitions = 680305 | |
Dealer showing 8 partitions = 680305 | |
Dealer showing 4 partitions = 680299 | |
Dealer showing 2 partitions = 658854 | |
Dealer showing 7 partitions = 680305 | |
Dealer showing 6 partitions = 680305 | |
Dealer showing 3 partitions = 679464 | |
Total partitions = 6398430 | |
{780434, 6398430} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule CardDeck do | |
def partitions(cards, subtotal) do | |
cards | |
|> Enum.with_index | |
|> do_partitions(cards, subtotal, 0) | |
end | |
defp do_partitions([], _cards, _subtotal, total) do | |
total | |
end | |
defp do_partitions([{card, _} | rest], cards, subtotal, total) when card <= 0 do | |
# discard this card | |
do_partitions(rest, cards, subtotal, total) | |
end | |
defp do_partitions([{_, index} | rest], cards, subtotal, total) do | |
case subtotal + index + 1 do | |
x when x > 21 -> | |
do_partitions(rest, cards, subtotal, total) | |
x when x < 21 -> | |
sum = partitions(decrement_card(cards, index), x) | |
do_partitions(rest, cards, subtotal, total + 1 + sum) | |
21 -> | |
do_partitions(rest, cards, subtotal, total + 1) | |
end | |
end | |
def decrement_card(deck, index) do | |
List.update_at(deck, index, fn _ -> Enum.at(deck, index) - 1 end) | |
end | |
def run do | |
deck = [4, 4, 4, 4, 4, 4, 4, 4, 4, 16] | |
d = | |
Enum.reduce(0..9, 0, fn i, acc1 -> | |
new_deck = decrement_card(deck, i) | |
p = | |
Enum.reduce(0..9, 0, fn (j, acc2) -> | |
new_deck | |
|> decrement_card(j) | |
|> partitions(j+1) | |
|> Kernel.+(acc2) | |
end) | |
IO.puts "Dealer showing #{i} partitions = #{p}" | |
acc1 + p | |
end) | |
IO.puts "Total partitions = #{d}" | |
d | |
end | |
def run_parallel do | |
deck = [4, 4, 4, 4, 4, 4, 4, 4, 4, 16] | |
d = | |
0..9 | |
|> Enum.map(fn i -> | |
Task.async(fn -> | |
new_deck = decrement_card(deck, i) | |
p = | |
Enum.reduce(0..9, 0, fn (j, acc2) -> | |
new_deck | |
|> decrement_card(j) | |
|> partitions(j+1) | |
|> Kernel.+(acc2) | |
end) | |
IO.puts "Dealer showing #{i} partitions = #{p}" | |
p | |
end) | |
end) | |
|> Enum.map(fn task -> Task.await(task) end) | |
|> Enum.sum | |
IO.puts "Total partitions = #{d}" | |
d | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Outcomes do | |
def partitions(cards, subtotal) do | |
Enum.sum( | |
for i <- 0..9, elem(cards,i)>0 do | |
cond do | |
subtotal+i+1 > 21 -> 0 | |
subtotal+i+1==21 -> 1 | |
subtotal+i+1 < 21 -> | |
1+partitions(put_elem(cards, i, elem(cards,i)-1), subtotal+i+1) | |
end | |
end | |
) | |
end | |
def run do | |
deck = Tuple.append(4 |> Tuple.duplicate(9), 16) | |
d = | |
Enum.sum( | |
for i <- 0..9 do | |
new_deck = put_elem(deck, i, elem(deck, i)-1) | |
p = | |
Enum.sum( | |
for j <- 0..9 do | |
Outcomes.partitions(put_elem(new_deck, j, elem(new_deck, j)-1), j+1) | |
end | |
) | |
IO.puts "Dealer showing #{i} partitions = #{p}" | |
p | |
end | |
) | |
IO.puts "Total partitions = #{d}" | |
d | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment