Last active
January 1, 2016 16:49
-
-
Save tijn/8173052 to your computer and use it in GitHub Desktop.
Trying to help Arjan
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
# this script proves that no matter how many bytes you leave out of the original string it won't result in a valid date that is in the specified range (within one year) | |
string = "\x04\x02R\xA6a\xC4M" | |
bytes = string.unpack("C*") | |
MIN = Time.new(2013,1,1) | |
MAX = Time.new(2013,12,31) | |
# equivalent to: | |
# string.bytes | |
def one_item_removed(array) | |
array.each_index do |i| | |
result = array.dup | |
result[i] = nil | |
yield result.compact | |
end | |
end | |
def two_items_removed(array, &block) | |
one_item_removed(array) do |a| | |
one_item_removed(array, &block) | |
end | |
end | |
def items_removed(array, n, &block) | |
case n | |
when 0 | |
block.call array | |
when 1 | |
one_item_removed(array, &block) | |
when 2..array.size | |
items_removed(array, n -1) do |x| | |
one_item_removed(x, &block) | |
end | |
else | |
raise "what? #{n}" | |
end | |
end | |
def time(bytes) | |
long = bytes.pack("C*").unpack("L*").first | |
return "nil" if long.nil? | |
t = Time.at long | |
in_range = t > MIN && t < MAX | |
raise "GEVONDEN!" if in_range | |
puts "#{t} #{'<===' if in_range}" | |
end | |
puts | |
puts "original" | |
puts time(bytes) | |
puts | |
puts "1 byte removed" | |
items_removed(bytes, 1) do |x| | |
puts time(x) | |
end | |
(2..bytes.size).each do |n| | |
puts | |
puts "#{n} bytes removed" | |
items_removed(bytes, n) do |x| | |
puts time(x) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm quite proud of line 33 :)