-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_custom_customs.rb
42 lines (37 loc) · 1021 Bytes
/
06_custom_customs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Only works for 32-bit integers,
# but since there are only 26 letters, it's fine.
def popcount32(v)
v = v - ((v >> 1) & 0x55555555)
v = (v & 0x33333333) + ((v >> 2) & 0x33333333)
(((v + (v >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24) & 0x3f
end
verboses = if ARGV.delete('-v')
[true, true]
elsif ARGV.delete('-v1')
[true, false]
elsif ARGV.delete('-v2')
[false, true]
end
a = ?a.ord
groups = ARGF.each("\n\n", chomp: true).map { |group_lines|
group_lines.each_line.map { |line|
# We could just store the chars,
# since | and & work on arrays,
# but I decided to use bitfields on principle.
#
# I didn't chomp, but "\n".ord - a is negative,
# so it contributes 0 to the sum.
line.chars.sum { |c| 1 << (c.ord - a) }
}.freeze
}.freeze
if verboses
puts "#{groups.size} groups"
puts "#{groups.map(&:size).tally}"
end
puts %i(| &).zip(verboses || [false, false]).map { |sym, verbose|
groups.sum { |group|
v = popcount32(group.reduce(sym))
p v if verbose
v
}
}