Created
October 27, 2024 18:54
-
-
Save ParadoxV5/80875a090ef46eb6242ef3f829aa6944 to your computer and use it in GitHub Desktop.
Ruby Discord Halloween 2024 Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VAMPIRES = ARGV.each_with_index.filter_map do|input, idx| | |
number = Integer input | |
length = input.length | |
next if length.odd? | |
fangs = input.chars.permutation(length).filter_map do|digits| | |
digits2 = digits.pop(length/2) | |
next if digits.last == '0' and digits2.last == '0' # This isn’t counted | |
a = Integer digits .join, 10 | |
b = Integer digits2.join, 10 | |
# Pairs will more often not multiply out than be a duplicate, so I check fang pairs before dedup. | |
next if a * b != number | |
# Duplication rate is at least 50% due to multiplication’s commutative property, | |
# so I use ordered tuples for their (slightly?) faster read speed. | |
a > b ? [b, a] : [a, b] | |
end | |
# I expect most of the input be non-vampires, so I dedicate them a fast-track | |
# (vs. utilizing `nil`ability of {Array#uniq!}) | |
next if fangs.empty? # not vampiric at all | |
fangs.uniq! | |
[ | |
~fangs.size, # main sorting key (descending) | |
idx, # stable sorting key (ascending) | |
input # original value – won’t actually participate in sorting, but is here hitching the ride | |
] | |
end | |
VAMPIRES.sort! | |
VAMPIRES.each { puts _3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment