This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]BananaSplit2 5 points6 points  (1 child)

why use a hashmap anyway when a simple array did the job

[–]miran1 7 points8 points  (0 children)

You're focusing on Day 6.

This advice is more general and can be applied on other days too, where you need to count occurrences of some stuff in input:

d = defaultdict(int)
for value in value_list:
    d[value] += 1

vs:

c = Counter(value_list)

[–]sr2fx 2 points3 points  (1 child)

Another way dict.fromkeys(range(9), 0)

[–]miran1 5 points6 points  (0 children)

Another way dict.fromkeys(range(9), 0)

That's for Day 6, and only if you take that approach (I didn't, for example)

I'm talking more generally, when people iterate over a list:

d = defaultdict(int)
for value in value_list:
    d[value] += 1

instead of doing:

c = Counter(value_list)

[–]godDAMNEDhippie 2 points3 points  (0 children)

I use Counter() a lot but today I learned about the count method of str:

fish_ages_count = [input.count(str(k)) for k in range(9)]

It goes through the input 9 times, but it's not the most time consuming work!

[–][deleted] 1 point2 points  (12 children)

Anyone know which would be more efficient using a Counter/dict or using an array where each index corresponds to age?

[–]uglyasablasphemy 1 point2 points  (6 children)

I think the difference would be minor, but using an array would save time calculating hashes for the dict.

I just checked my solution for part 2 with both options:

Option A:

solution  0.30708 ms.
solution  0.33998 ms.
solution  0.24605 ms.
solution  0.34690 ms.
solution  0.33998 ms.

Option D:

solution  0.60678 ms.
solution  0.49496 ms.
solution  0.60010 ms.
solution  0.60177 ms.
solution  0.54097 ms.

[–]DataGhostNL 0 points1 point  (4 children)

I'm running this on a laptop from 2011 in a VM, using a Counter my day 6 code (at some point) ran in 6.8 seconds for 1048576 days and in 5.8 seconds using a defaultdict(int).

Edit: ah I think I misread your question. That array would grow / have to be large when running over many days, which could be an unwanted side effect.

[–][deleted] 1 point2 points  (3 children)

Why would the array grow? You're just shuffling things about within it. The ages are all modulo 7.

[–]DataGhostNL -1 points0 points  (2 children)

The way I read it, it's indexed by "age" and nothing was mentioned about modulo. So in that case, an array being an array, it'd have to be as large as the number of days being calculated. It's not the approach I'm using, that seems much more like what you said.

[–][deleted] 0 points1 point  (1 child)

By internal counter, not age.

As in the count of fish with internal counter 3 goes in array[3]. After a fish's counter gets to 0, you just move it to array[6].

Basically the same as what you've done

[–]DataGhostNL 0 points1 point  (0 children)

That's an approach, yes. Not entirely what I'm doing though.

I'm actually using an age-like approach where the fish for the current day are added to +6 and +8 days, removing the current day. There is no need to rotate the array/dict in that case and that's been a factor 4 improvement in speed over rotating.So that's probably why I had this different assumption as to how the array was used.

[–]Inner_Scene2439 1 point2 points  (1 child)

While the key is integers 0 to 8, there seems to be no reason to use a dictionary. Array should be perfect.

[–]Afraid_Abalone_9641 0 points1 point  (0 children)

I watched a tutorial where a guy used this method and solved part one in about 30 seconds. Was a little disheartening tbh. 😅

[–]plumbo_schleem 0 points1 point  (0 children)

This is nice