lizthegrey / adventofcode

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question around code day 17

omarjuul opened this issue · comments

Hey there, I'm not sure if this is the place to ask stuff like this, but I have a question about your code for day 17. If there is a better place to ask questions then I apologize and please direct me to it :)
I'm trying to learn a bit of the go language by viewing your code, so I'm wondering if there's something I'm misunderstanding:

if _, exists := neighCount[k]; !exists {

Is the initialization here actually doing anything? It seems to me that the increment a few lines down also hits keys that have not been initialized by this loop (yet). Am I missing something?

(As an aside: I think you eliminated the need for the ActiveNeighbors() method (is it called a method in go?) when you introduced the neighCount map, so that might not be needed in the code anymore?)

Not sure if this is seen so I'll ping @lizthegrey ...

Is the initialization here actually doing anything? It seems to me that the increment a few lines down also hits keys that have not been initialized by this loop (yet). Am I missing something?

Ah, the reason for this is that normally golang defaults missing keys to 0 on first access, but there is a difference between explicitly set vs missing keys for iteration purposes. Down below, I need the range neighCount to contain any active cells, even those that have 0 active neighbors, so that we can properly turn off singletons that have drifted off on their on and should be turned off :)

(As an aside: I think you eliminated the need for the ActiveNeighbors() method (is it called a method in go?) when you introduced the neighCount map, so that might not be needed in the code anymore?)

Good catch. Yes, the function can be eliminated.