Using Enumerable#group_by simplifies counting