amulets/extract_poems.rb

73 lines
1.8 KiB
Ruby
Raw Normal View History

2024-11-14 00:31:51 -05:00
# frozen_string_literal: true
2024-11-14 13:20:10 -05:00
require 'digest'
MIN_8S = 5
def expand(set)
cased = set + set.map(&:downcase) + set.map(&:upcase)
spaced = cased + cased.map { _1.gsub(/\s/, ' ') } + cased.map { _1.gsub(/\s/, "\n") } + cased.flat_map do |amulet|
20.times.map { amulet.gsub(/\s/) { rand(2).zero? ? ' ' : "\n" } }
end
spaced + spaced.flat_map { _1.match?(/[.?!]$/) ? [_1.gsub('.', '?'), _1.gsub('?', '.')] : ["#{_1}.", "#{_1}?"] }
end
2024-11-14 00:31:51 -05:00
def max_8s(str)
(str.scan(/8+/).max || '').length
end
2024-11-14 13:20:10 -05:00
existing = Set[
"If you can't write poems,
write me",
"IN THE SPRING MY LUNGS
STILL SOMEHOW EXPAND.",
"A MAN ONCE MAILED ME
A PIECE OF HIS HEART",
"THIS AMULET
AT ANY PRICE
FELT LIKE THE TRUTH",
"this amulet is a simple token which proves my love's truth",
"DON'T WORRY.",
"in the early hours of the new year
i lie on my back
waiting.",
"All my life I had this
image of what a poet
should be",
'chaiyya chaiyya',
'THE REAL AMULET IS THE FRIENDS WE MADE ALONG THE WAY*',
'Winter evening, a leaf, a blue sky above.',
'Lethargically good.',
'An amulet is a self-aware house?',
"Ever Tried Ever Failed
Try Again Fail Again. find your amulet",
'the wind at night has a dream',
"the wind:
don't ask me what it is",
'a certain wind, to blow this back to me',
]
existing = expand existing
poems = $stdin.read.split('poem: ')
.map(&:strip)
.reject(&:empty?)
.to_set
poems = expand(poems) - existing
amulets = poems
.map { [_1, Digest::SHA256.hexdigest(_1)] }
.filter { |(_, hash)| max_8s(hash) >= MIN_8S }
.sort_by { |(_, hash)| max_8s hash }
puts "#{amulets.length}/#{poems.length} poems were amulets"
puts
amulets.each do |(amulet, hash)|
puts amulet
puts
puts "#{max_8s hash} #{hash}#{amulet.length > 64 ? ' too long!' : ''}"
puts
end