0


(This is Advent Of Code day 4, I already have my stars, but I'd like to do it more efficiently)
I have a 3d matrix like this one:

[
    [
        [{22: 0}, {13: 0}, {17: 1}, {11: 1}, {0 : 1}],
        [{8 : 0}, {2 : 1}, {23: 1}, {4 : 1}, {24: 1}],
        [{21: 1}, {9 : 1}, {14: 1}, {16: 0}, {7 : 1}],
        [{6 : 0}, {10: 0}, {3 : 0}, {18: 0}, {5 : 1}],
        [{1 : 0}, {12: 0}, {20: 0}, {15: 0}, {19: 0}]
    ],

    [
        [{3 : 0}, {15: 0}, {0 : 1}, {2 : 1}, {22: 0}],
        [{9 : 1}, {18: 0}, {13: 0}, {17: 1}, {5 : 1}],
        [{19: 0}, {8 : 0}, {7 : 1}, {25: 0}, {23: 1}],
        [{20: 0}, {11: 1}, {10: 0}, {24: 1}, {4 : 1}],
        [{14: 1}, {21: 1}, {16: 0}, {12: 0}, {6 : 0}]
    ], 
    
    [
        [{14: 1}, {21: 1}, {17: 1}, {24: 1}, {4 : 1}],
        [{10: 0}, {16: 0}, {15: 0}, {9 : 1}, {19: 0}],
        [{18: 0}, {8 : 0}, {23: 1}, {26: 0}, {20: 0}], 
        [{22: 0}, {11: 1}, {13: 0}, {6 : 0}, {5 : 1}], 
        [{2 : 1}, {0 : 1}, {12: 0}, {3 : 0}, {7 : 1}]
    ]
]

And I want to sum all values from one of those matrices using map/sum functions. This is what I have now and works:

# z is one of those matrices, 0, 1 or 2
soma = 0
for line in matrices[z]:  # line is a list of dictionaries
    for dic in line:      # dic is a dictionary from the list
        valor = list(dic.values())[0]
        if valor == 0:
            soma += list(dic.keys())[0]

What I've tried to do:

print("soma = ", sum(map(sum, ( (value.values())[0] for value in matrizes[z]))))

which doesnt work, and I get this error: Traceback (most recent call last):

File "day4.py", line 75, in <module>
print("part 1 = ", sum(map(sum, ((value.values())[0] for value in matrizes[z]))))
File "day4.py", line 75, in <genexpr>
print("part 1 = ", sum(map(sum, ((value.values())[0] for value in matrizes[z]))))
AttributeError: 'list' object has no attribute 'values'

I've found a post with 2d arrays, but couldnt understand how to make it for 3d arrays.
I've also found posts using "numpy", but I want to do with map and sum functions.

arthurmluz
  • 28
  • 3
  • Not to say this is wrong, but it's really awkward to have dictionaries in numpy arrays, it kinda defeats the purpose of all the useful matrix operations you can use with numpy. A better solution imo is to "mark" values by setting them to -1. Basically every time a new bingo value appears, you set that value to -1 in the matrices. Any col/row with a sum of -5 for a given matrix would be a bingo. – Nathan Furnal Dec 06 '21 at 23:35
  • @NathanFurnal first: I'm not using numpy. second: I'm setting the values to 1 and if the sum is +5, it's bingo. I just want to do the sum more efficiently – arthurmluz Dec 07 '21 at 03:31
  • Aight my bad, I thought you wanted to because of the phrasing of the question. – Nathan Furnal Dec 07 '21 at 09:12

2 Answers2

1

You can do one line like this:

m = [
    [
        [{22: 0}, {13: 0}, {17: 1}, {11: 1}, {0 : 1}],
        [{8 : 0}, {2 : 1}, {23: 1}, {4 : 1}, {24: 1}],
        [{21: 1}, {9 : 1}, {14: 1}, {16: 0}, {7 : 1}],
        [{6 : 0}, {10: 0}, {3 : 0}, {18: 0}, {5 : 1}],
        [{1 : 0}, {12: 0}, {20: 0}, {15: 0}, {19: 0}]
    ],

    [
        [{3 : 0}, {15: 0}, {0 : 1}, {2 : 1}, {22: 0}],
        [{9 : 1}, {18: 0}, {13: 0}, {17: 1}, {5 : 1}],
        [{19: 0}, {8 : 0}, {7 : 1}, {25: 0}, {23: 1}],
        [{20: 0}, {11: 1}, {10: 0}, {24: 1}, {4 : 1}],
        [{14: 1}, {21: 1}, {16: 0}, {12: 0}, {6 : 0}]
    ], 
    
    [
        [{14: 1}, {21: 1}, {17: 1}, {24: 1}, {4 : 1}],
        [{10: 0}, {16: 0}, {15: 0}, {9 : 1}, {19: 0}],
        [{18: 0}, {8 : 0}, {23: 1}, {26: 0}, {20: 0}], 
        [{22: 0}, {11: 1}, {13: 0}, {6 : 0}, {5 : 1}], 
        [{2 : 1}, {0 : 1}, {12: 0}, {3 : 0}, {7 : 1}]
    ]
]

s = sum([k for y in m for x in y for z in x for k,v in z.items() if v == 0])
print(s)
mama
  • 2,046
  • 1
  • 7
  • 24
  • If you want keys you dont even have to write .anything() just remove values – mama Dec 06 '21 at 23:29
  • Thanks for the help! it worked for the sum of all items on the matrices, I'll keep that in mind. I was looking for summing just one of those matrices, like m[z], the other answer helped with that. – arthurmluz Dec 07 '21 at 13:59
1

You can do:

z = 0
soma = sum(map(lambda line: sum(k for d in line for k, v in d.items() if v == 0), matrices[z]))
print(soma) # 163
j1-lee
  • 13,764
  • 3
  • 14
  • 26