Skip to content

Commit e290342

Browse files
committed
Day 14: Map nibbles without to_i -> to_s
Makes no appreciable difference in runtime. Apparently all the slowness is performing all of the 128 * 64 * (9 + 5 + n) rotations.
1 parent d1db68b commit e290342

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

14_disk_defrag.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
GRID = 128
55

6+
NIBBLE = (0...16).to_h { |x|
7+
bits = [8, 4, 2, 1].map { |bit| x & bit != 0 }
8+
[x.to_s(16), bits.freeze]
9+
}.freeze
10+
611
grid = (0...GRID).map { |n|
712
KnotHash::hash(
813
"#{!ARGV.empty? && !File.exist?(ARGV.first) ? ARGV.first : ARGF.read}-#{n}".bytes + KnotHash::SUFFIX
9-
).to_i(16).to_s(2).rjust(GRID, ?0).each_char.map { |x| x == ?1 }
14+
).each_char.flat_map(&NIBBLE)
1015
}
1116

1217
puts grid.sum { |row| row.count(true) }

0 commit comments

Comments
 (0)