# Reducing into map in two steps not equivalent to combining steps

Can someone explain why expand_map and expand_map2 are not equivalent below? Since the Map.put/3 call is being called with distinct keys, I can’t see why they should be different.

``````defmodule Day16Weirdness do
def expand_map(map, width, height) do
map
|> Enum.reduce(map, fn {{r, c}, risk}, map ->
1..4
|> Enum.reduce(map, fn i, map ->
Map.put(map, {r + i * width, c}, increment_risk(risk, i))
end)
end)
|> Enum.reduce(map, fn {{r, c}, risk}, map ->
1..4
|> Enum.reduce(map, fn i, map ->
Map.put(map, {r, c + i * height}, increment_risk(risk, i))
end)
end)
end

def expand_map2(map, width, height) do
map
|> Enum.reduce(map, fn {{r, c}, risk}, map ->
1..4
|> Enum.reduce(map, fn i, map ->
Map.put(map, {r + i * width, c}, increment_risk(risk, i))
|> Map.put({r, c + i * height}, increment_risk(risk, i))
end)
end)
end

defp increment_risk(risk, i) do
x = risk + i
if x > 9 do
x - 9
else
x
end
end
end
##############
map = for r <- 0..4,
c <- 0..4 do
{r,c}
end
|> Enum.zip(1..25 |> Enum.map(&rem(&1, 10)))
|> Enum.into(%{})

new_map = expand_map(map, 5, 5) |> IO.inspect(limit: :infinity)
new_map2 = expand_map2(map, 5, 5) |> IO.inspect(limit: :infinity)
(new_map == new_map2) |> IO.inspect()
``````

Here’s the output:

Summary

This text will be hidden

``````%{
{4, 5} => 2,
{19, 22} => 1,
{16, 10} => 2,
{11, 8} => 3,
{11, 19} => 5,
{16, 24} => 7,
{6, 18} => 4,
{17, 11} => 7,
{15, 17} => 9,
{17, 7} => 7,
{10, 13} => 8,
{5, 9} => 7,
{12, 16} => 7,
{14, 16} => 7,
{6, 23} => 5,
{17, 22} => 1,
{10, 19} => 1,
{14, 15} => 6,
{3, 15} => 9,
{14, 17} => 8,
{13, 6} => 1,
{4, 19} => 8,
{12, 8} => 7,
{5, 17} => 7,
{3, 16} => 1,
{23, 8} => 5,
{5, 22} => 8,
{1, 2} => 8,
{16, 15} => 3,
{15, 16} => 8,
{8, 5} => 8,
{1, 24} => 4,
{23, 23} => 8,
{18, 10} => 2,
{4, 15} => 4,
{14, 24} => 2,
{4, 18} => 7,
{21, 24} => 8,
{24, 15} => 8,
{14, 8} => 7,
{6, 21} => 3,
{3, 18} => 3,
{0, 9} => 6,
{10, 23} => 1,
{7, 17} => 7,
{8, 6} => 9,
{24, 23} => 3,
{13, 22} => 5,
{23, 24} => 8,
{8, 11} => 1,
{15, 8} => 8,
{24, 9} => 1,
{8, 10} => 9,
{3, 6} => 8,
{2, 16} => 5,
{16, 23} => 7,
{3, 17} => 2,
{6, 24} => 5,
{3, 12} => 1,
{6, 15} => 1,
{7, 16} => 6,
{2, 4} => 5,
{8, 17} => 3,
{16, 18} => 6,
{18, 23} => 7,
{19, 7} => 7,
{4, 8} => 5,
{24, 13} => 1,
{17, 16} => 8,
{16, 8} => 4,
{4, 12} => 5,
{23, 21} => 6,
{24, 21} => 1,
{23, 14} => 6,
{13, 13} => 4,
{19, 16} => 8,
{24, 16} => 9,
{10, 5} => 4,
{6, 5} => 8,
{20, 21} => 1,
{17, 24} => 3,
{18, 24} => 7,
{19, 12} => 8,
{21, 19} => 7,
{18, 22} => 6,
{13, 12} => 3,
{8, 14} => 3,
{15, 18} => 1,
{12, 17} => 8,
{19, 10} => 6,
{21, 21} => 6,
{8, 22} => 4,
{18, 5} => 1,
{0, 3} => 4,
{1, 1} => 7,
{9, 11} => 5,
{2, 20} => 5,
{13, 20} => 3,
{5, 23} => 9,
{17, 18} => 1,
{9, 6} => 4,
{11, 22} => 5,
{20, 7} => 8,
{4, 3} => 4,
{1, 22} => 3,
{14, 5} => 4,
{17, 23} => 2,
{24, 12} => 9,
{13, 23} => 6,
{3, 7} => 9,
{9, 13} => 7,
{13, 10} => 1,
{15, 14} => 1,
{22, 8} => 9,
{19, 8} => 8,
{5, 10} => 4,
{0, 5} => 2,
{22, 17} => 1,
{6, 10} => 9,
{0, 1} => 2,
{18, 21} => 5,
{1, 16} => 1,
{20, 5} => 6,
{8, 9} => 2,
{8, 19} => 4,
{20, 22} => 2,
{8, 20} => 2,
{21, 12} => 5,
{21, 20} => 5,
{4, 0} => 1,
{3, 14} => 2,
{7, 19} => 9,
{3, 2} => 8,
{8, 24} => 5,
{19, 6} => 6,
{9, 8} => 6,
{2, 21} => 6,
{24, 14} => 2,
{14, 19} => 1,
{2, 13} => 6,
{7, 9} => 7,
{24, 20} => 9,
{24, 10} => 7,
{0, 8} => 5,
{7, 15} => 5,
{22, 12} => 9,
{9, 18} => 8,
{7, 21} => 7,
{12, 10} => 5,
{4, 20} => 5,
{11, 5} => 9,
{4, 13} => 6,
{3, 1} => 7,
{24, 19} => 3,
{5, 16} => 6,
{3, 23} => 4,
{21, 18} => 7,
{10, 20} => 7,
{22, 24} => 4,
{2, 0} => 1,
{21, 5} => 2,
{10, 17} => 8,
{10, 16} => 7,
{19, 18} => 1,
{15, 12} => 8,
{12, 13} => 8,
{17, 17} => 9,
{19, 11} => 7,
{21, 10} => 3,
{21, 16} => 5,
{15, 15} => 7,
{10, 8} => 7,
{2, 11} => 4,
{11, 23} => 6,
{11, 24} => 6,
{8, 12} => 2,
{19, 20} => 8,
{12, 15} => 6,
{22, 11} => 8,
{15, 10} => 6,
{7, 18} => 8,
{2, 7} => 4,
{4, 6} => 3,
{19, 5} => 5,
{21, 22} => 7,
{12, 23} => 1,
{15, 6} => 6,
{21, 17} => 6,
{19, 9} => 9,
{16, 20} => 4,
{6, 17} => 3,
{23, 9} => 5,
{17, 5} => 5,
{13, 21} => 4,
{17, 10} => 6,
{6, 20} => 2,
{0, 7} => 4,
{0, 16} => 5,
{10, 15} => 6,
{17, 14} => 1,
{9, 20} => 6,
{5, 20} => 6,
{24, 11} => 8,
{4, 23} => 8,
{21, 9} => 5,
{8, 23} => 5,
{2, 10} => 3,
{3, 11} => 9,
{18, 11} => 3,
{4, 11} => 4,
{2, 22} => 7,
{4, 10} => 3,
{9, 19} => 9,
{7, 23} => 9,
{15, 11} => 7,
{0, 0} => 1,
{8, 7} => 1,
{14, 10} => 5,
{5, 8} => 6,
{7, 6} => 4,
{3, 20} => 1,
{0, 10} => 3,
{17, 12} => 8,
{22, 15} => 8,
{18, 20} => 4,
{22, 10} => 7,
{19, 23} => 2,
{2, 8} => 5,
{19, 19} => 2,
{1, 4} => 0,
{23, 6} => 3,
{10, 6} => 5,
{14, 18} => 9,
{22, 22} => 2,
{22, 9} => 1,
{9, 21} => 7,
{12, 18} => 9,
{11, 14} => 4,
{6, 19} => 4,
{9, 22} => 8,
{20, 8} => 9,
{2, 17} => 6,
{12, 7} => 6,
{18, 14} => 5,
{5, 6} => 4,
{22, 21} => 1,
{9, 5} => 3,
{1, 11} => 9,
{7, 22} => 8,
{20, 10} => 7,
{6, 6} => 9,
{17, 15} => 7,
{23, 18} => 7,
{5, 11} => 5,
{12, 19} => 1,
{4, 14} => 7,
{17, 6} => 6,
{20, 23} => 3,
{14, 20} => 7,
{0, 4} => 5,
{21, 6} => 3,
{22, 13} => 1,
{5, 19} => 9,
{9, 16} => 6,
{13, 24} => 6,
{6, 8} => 2,
{1, 7} => 9,
{4, 2} => 3,
{13, 15} => 2,
{15, 9} => 9,
{24, 18} => 2,
{24, 24} => 4,
{13, 8} => 3,
{15, 7} => 7,
{2, 19} => 8,
{1, 19} => 3,
{13, 18} => 5,
{22, 18} => 2,
{22, 23} => 3,
{8, 8} => 2,
{2, 15} => 4,
{13, 9} => 3,
{11, 12} => 3,
{22, 20} => 9,
{2, 3} => 4,
{2, 23} => 8,
{16, 14} => 5,
{17, 21} => 9,
{18, 9} => 4,
{16, 17} => 5,
{15, 5} => 5,
{23, 12} => 5,
{13, 17} => 4,
{1, 8} => 1,
{12, 5} => 4,
{11, 10} => 1,
{20, 13} => 1,
{16, 19} => 6,
{15, 23} => 2,
{3, 4} => 0,
{10, 21} => 8,
{12, 22} => 9,
{23, 13} => 6,
{20, 19} => 3,
{10, 24} => 2,
{21, 13} => 6,
{17, 8} => 8,
{7, 5} => 3,
{19, 14} => 1,
{15, 19} => 2,
{16, 5} => 1,
{1, 23} => 4,
{9, 24} => 1,
{23, 11} => 4,
{17, 13} => 9,
{23, 10} => 3,
{14, 13} => 8,
{1, 15} => 9,
{11, 7} => 2,
{18, 6} => 2,
{22, 7} => 8,
{24, 6} => 7,
{11, 9} => 3,
{23, 22} => 7,
{14, 9} => 8,
{2, 1} => 2,
{24, 8} => 9,
{4, 16} => 5,
{3, 19} => 3,
{23, 20} => 5,
{5, 13} => 7,
{11, 17} => 4,
{3, 10} => 8,
{14, 11} => 6,
{3, 22} => 3,
{1, 14} => 2,
{7, 8} => 6,
{0, 21} => 6,
{22, 14} => 2,
{14, 23} => 1,
{9, 14} => 8,
{18, 19} => 6,
{4, 7} => 4,
{14, 14} => 9,
{12, 12} => 7,
{1, 21} => 2,
{23, 16} => 5,
{20, 6} => 7,
{11, 20} => 3,
{3, 3} => 9,
{20, 11} => 8,
{13, 7} => 2,
{21, 8} => 5,
{23, 15} => 4,
{1, 10} => 8,
{13, 5} => 9,
{10, 9} => 8,
{3, 0} => 6,
{21, 11} => 4,
{6, 14} => 3,
{20, 18} => 2,
{4, 21} => 6,
{7, 11} => 5,
{4, 9} => 6,
{9, 15} => 5,
{6, 13} => 3,
{24, 22} => 2,
{10, 7} => 6,
{22, 19} => 3,
{16, 16} => 4,
{17, 9} => 9,
{12, 20} => 7,
{7, 20} => 6,
{11, 6} => 1,
{11, 21} => 4,
{20, 17} => 1,
{6, 16} => 2,
{13, 14} => 4,
{11, 16} => 3,
{7, 7} => 5,
{10, 12} => 7,
{0, 17} => 6,
{20, 16} => 9,
{15, 24} => 3,
{10, 14} => 9,
{14, 6} => 5,
{19, 13} => 9,
{6, 22} => 4,
{16, 6} => 2,
{17, 20} => 8,
{11, 18} => 5,
{2, 24} => 9,
{16, 7} => 3,
{8, 16} => 2,
{14, 7} => 6,
{10, 10} => 5,
{9, 10} => 4,
{14, 21} => 8,
{19, 24} => 3,
{18, 18} => 6,
{20, 9} => 1,
{20, 20} => 9,
{8, 13} => 3,
{18, 16} => 4,
{21, 14} => 6,
{8, 18} => 4,
{20, 24} => 4,
{0, 19} => 8,
{1, 6} => 8,
{4, 1} => 2,
{5, 21} => 7,
{18, 12} => 4,
{5, 5} => 3,
{6, 12} => 2,
{5, 14} => 8,
{7, 14} => 8,
{7, 12} => 6,
{16, 22} => 6,
{15, 22} => 1,
{18, 17} => 5,
{1, 9} => 1,
{21, 15} => 4,
{4, 24} => 9,
{8, 21} => 3,
{13, 16} => 3,
{0, 22} => 7,
{1, 12} => 1,
{22, 6} => 7,
{9, 23} => 9,
{16, 13} => 5,
{16, 11} => 3,
{19, 17} => 9,
{12, 24} => 2,
{19, 21} => 9,
{1, 17} => 2,
{12, 6} => 5,
{5, 24} => 1,
{3, 5} => 7,
{0, 24} => 9,
{1, 0} => 6,
{2, 14} => 7,
{5, 18} => 8,
{3, 13} => 2,
{0, 15} => 4,
{0, 11} => 4,
{23, 5} => 2,
{1, 13} => 2,
{11, 15} => 2,
{4, 22} => 7,
{2, 6} => 3,
{12, 9} => 8,
{21, 7} => 4,
{1, 5} => 7,
{23, 17} => 6,
{14, 22} => 9,
{15, 13} => 9,
{7, 24} => 1,
{18, 8} => 4,
{2, 12} => 5,
{21, 23} => 8,
{1, 18} => 3,
{20, 14} => 2,
{13, 19} => 5,
{9, 7} => 5,
{7, 10} => 4,
{12, 14} => 9,
{24, 5} => 6,
{24, 17} => 1,
{23, 19} => 7,
{2, 18} => 7,
{15, 20} => 8,
{6, 11} => 1,
{2, 5} => 2,
{15, 21} => 9,
{13, 11} => 2,
{0, 20} => 5,
{0, 13} => 6,
{18, 7} => 3,
{12, 11} => 6,
{8, 15} => 1,
{2, 2} => 3,
{5, 7} => 5,
{6, 9} => 2,
{0, 2} => 3,
{22, 16} => 9,
{18, 15} => 3,
{9, 12} => 6,
{4, 4} => 5,
{22, 5} => 6,
{0, 6} => 3,
{16, 21} => 5,
{6, 7} => 1,
{10, 11} => 6,
{0, 12} => 5,
{3, 8} => 1,
{3, 24} => 4,
{11, 13} => 4,
{17, 19} => 2,
{10, 22} => 9,
{0, 18} => 7,
{9, 17} => 7,
{5, 12} => 6,
{12, 21} => 8,
{19, 15} => 7,
{14, 12} => 7,
{0, 14} => 7,
{7, 13} => 7,
{0, 23} => 8,
{16, 9} => 4,
{16, 12} => 4,
{11, 11} => 2,
{20, 15} => 8,
{4, 17} => 6,
{18, 13} => 5,
{1, 3} => 9,
{1, 20} => 1,
{9, 9} => 7,
{3, 9} => 1,
{5, 15} => 5,
{24, 7} => 8,
{20, 12} => 9,
{2, 9} => 6,
{3, 21} => 2,
{10, 18} => 9,
{23, 7} => 4
}
%{
{4, 5} => 2,
{20, 3} => 8,
{17, 0} => 4,
{3, 15} => 9,
{4, 19} => 8,
{3, 16} => 1,
{15, 3} => 7,
{1, 2} => 8,
{15, 2} => 6,
{1, 24} => 4,
{4, 15} => 4,
{4, 18} => 7,
{12, 3} => 6,
{3, 18} => 3,
{0, 9} => 6,
{16, 1} => 1,
{18, 3} => 3,
{10, 0} => 3,
{5, 2} => 4,
{3, 6} => 8,
{2, 16} => 5,
{3, 17} => 2,
{12, 1} => 4,
{3, 12} => 1,
{2, 4} => 5,
{24, 1} => 6,
{4, 8} => 5,
{19, 1} => 5,
{11, 2} => 1,
{4, 12} => 5,
{24, 2} => 7,
{22, 1} => 6,
{14, 3} => 6,
{0, 3} => 4,
{1, 1} => 7,
{2, 20} => 5,
{22, 4} => 9,
{4, 3} => 4,
{1, 22} => 3,
{3, 7} => 9,
{16, 2} => 2,
{13, 2} => 1,
{5, 0} => 2,
{11, 3} => 2,
{0, 5} => 2,
{0, 1} => 2,
{1, 16} => 1,
{24, 0} => 5,
{4, 0} => 1,
{13, 1} => 9,
{17, 4} => 8,
{3, 14} => 2,
{3, 2} => 8,
{21, 4} => 4,
{10, 3} => 6,
{2, 21} => 6,
{8, 1} => 8,
{2, 13} => 6,
{7, 3} => 5,
{0, 8} => 5,
{4, 20} => 5,
{4, 13} => 6,
{3, 1} => 7,
{16, 4} => 3,
{6, 1} => 8,
{17, 3} => 7,
{3, 23} => 4,
{2, 0} => 1,
{2, 11} => 4,
{8, 3} => 1,
{8, 4} => 1,
{23, 1} => 2,
{2, 7} => 4,
{4, 6} => 3,
{23, 2} => 3,
{9, 4} => 6,
{22, 2} => 7,
{6, 2} => 9,
{23, 3} => 4,
{0, 7} => 4,
{0, 16} => 5,
{9, 0} => 2,
{4, 23} => 8,
{7, 2} => 4,
{2, 10} => 3,
{3, 11} => 9,
{4, 11} => 4,
{2, 22} => 7,
{4, 10} => 3,
{0, 0} => 1,
{12, 4} => 7,
{3, 20} => 1,
{23, 0} => 1,
{0, 10} => 3,
{17, 2} => 6,
{18, 0} => 9,
{2, 8} => 5,
{1, 4} => 0,
{18, 2} => 2,
{2, 17} => 6,
{19, 4} => 8,
{1, 11} => 9,
{4, 14} => 7,
{9, 3} => 5,
{0, 4} => 5,
{21, 0} => 1,
{1, 7} => 9,
{4, 2} => 3,
{2, 19} => 8,
{1, 19} => 3,
{23, 4} => 4,
{2, 15} => 4,
{2, 3} => 4,
{2, 23} => 8,
{11, 1} => 9,
{1, 8} => 1,
{10, 4} => 7,
{8, 2} => 9,
{24, 4} => 9,
{3, 4} => 0,
{14, 1} => 4,
{20, 0} => 5,
{18, 1} => 1,
{1, 23} => 4,
{12, 2} => 5,
{1, 15} => 9,
{2, 1} => 2,
{15, 1} => 5,
{4, 16} => 5,
{3, 19} => 3,
{7, 0} => 2,
{11, 0} => 8,
{3, 10} => 8,
{3, 22} => 3,
{1, 14} => 2,
{0, 21} => 6,
{20, 2} => 7,
{4, 7} => 4,
{1, 21} => 2,
{3, 3} => 9,
{1, 10} => 8,
{21, 3} => 4,
{3, 0} => 6,
{4, 21} => 6,
{17, 1} => 5,
{4, 9} => 6,
{9, 1} => 3,
{0, 17} => 6,
{13, 4} => 2,
{20, 4} => 9,
{11, 4} => 2,
{6, 0} => 7,
{2, 24} => 9,
{7, 1} => 3,
{22, 0} => 5,
{5, 3} => 5,
{15, 4} => 8,
{0, 19} => 8,
{1, 6} => 8,
{4, 1} => 2,
{21, 1} => 2,
{8, 0} => 7,
{1, 9} => 1,
{4, 24} => 9,
{0, 22} => 7,
{1, 12} => 1,
{9, 2} => 4,
{19, 0} => 4,
{10, 1} => 4,
{6, 4} => 1,
{1, 17} => 2,
{3, 5} => 7,
{18, 4} => 3,
{0, 24} => 9,
{1, 0} => 6,
{2, 14} => 7,
{13, 0} => 8,
{3, 13} => 2,
{0, 15} => 4,
{0, 11} => 4,
{16, 0} => 9,
{1, 13} => 2,
{4, 22} => 7,
{2, 6} => 3,
{1, 5} => 7,
{19, 2} => 6,
{2, 12} => 5,
{12, 0} => 3,
{13, 3} => 2,
{1, 18} => 3,
{20, 1} => 6,
{19, 3} => 7,
{16, 3} => 3,
{21, 2} => 3,
{5, 1} => 3,
{14, 0} => 3,
{2, 18} => 7,
{2, 5} => 2,
{0, 20} => 5,
{10, 2} => 5,
{0, 13} => 6,
{24, 3} => 8,
{2, 2} => 3,
{14, 2} => 5,
{0, 2} => 3,
{4, 4} => 5,
{7, 4} => 6,
{0, 6} => 3,
{0, 12} => 5,
{3, 8} => 1,
{3, 24} => 4,
{14, 4} => 7,
{0, 18} => 7,
{6, 3} => 1,
{0, 14} => 7,
{0, 23} => 8,
{15, 0} => 4,
{5, 4} => 6,
{22, 3} => 8,
{4, 17} => 6,
{1, 3} => 9,
{1, 20} => 1,
{3, 9} => 1,
{2, 9} => 6,
{3, 21} => 2
}
false

``````
1 Like

You should no be using the same variable name for everything, as you will totally loose track of what is going on with your code.

for your first expand, `map` is your enumerable, your accumulator, then you pipe again on it but your accumulator is your original `map` so you loose everything from the first reduce, but the numbers elements added is the total number of elements from the result of the first reduce.

4 Likes

Here’s a rewrite of your code.
expand_map is not the same as your original as there were some stuff that didn’t make any sense… I tried to correct, like the second reduce is:
`Enum.reduce(result1, result1, ...` where in your original code you were doing `Enum.reduce(result1, map, ...`

I hope you can see why expand_map and expand_map2 are totally different.

``````defmodule Day16Weirdness do
def expand_map(map, width, height) do
result1 =
Enum.reduce(map, map, fn {{r, c}, risk}, acc1 ->
Enum.reduce(1..4, acc1, fn i, acc2 ->
Map.put(acc2, {r + i * width, c}, increment_risk(risk, i))
end)
end)

result2 =
Enum.reduce(result1, result1, fn {{r, c}, risk}, acc1 ->
Enum.reduce(1..4, acc1, fn i, acc2 ->
Map.put(acc2, {r, c + i * height}, increment_risk(risk, i))
end)
end)

result2
end

def expand_map2(map, width, height) do
Enum.reduce(map, map, fn {{r, c}, risk}, acc1 ->
Enum.reduce(1..4, acc1, fn i, acc2 ->
acc2
|> Map.put({r + i * width, c}, increment_risk(risk, i))
|> Map.put({r, c + i * height}, increment_risk(risk, i))
end)
end)
end

defp increment_risk(risk, i) do
x = risk + i

if x > 9 do
x - 9
else
x
end
end
end
``````
3 Likes