tests.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. require 'pp'
  2. require 'set'
  3. CAPTURES_DIR = File.expand_path(File.join(__FILE__, "../../packet_captures"))
  4. def first_byte_determines_next(capture, count)
  5. capture
  6. .group_by { |x| x[0] }
  7. .map { |_, x| x.map { |packet| packet[1..count] } }
  8. .map(&:uniq)
  9. .map(&:length)
  10. .all? { |x| x == 1 }
  11. end
  12. def parse_capture(filename)
  13. File
  14. .read(filename)
  15. .split("\n")
  16. .map { |x| x.split(' ').map { |b| b.to_i(16) } }
  17. end
  18. Dir.glob("#{CAPTURES_DIR}/**.txt").each do |filename|
  19. puts File.basename(filename)
  20. capture = parse_capture(filename)
  21. pp first_byte_determines_next(capture, 3)
  22. # a = capture
  23. # .group_by { |x| x[0] }
  24. # .sort
  25. # .map { |_, packets| [packets.first[0], packets.first[1]] }
  26. # mappings = Hash[a]
  27. # perms = []
  28. # current_perm = []
  29. # elem = a.first
  30. # current_perm = [elem[0]]
  31. # visited = Set.new([elem[0]])
  32. # perm_visited = Set.new([elem[0]])
  33. #
  34. # while visited.length < mappings.length
  35. # new_elem = nil
  36. #
  37. # puts visited.inspect
  38. #
  39. # if !mappings.include?(elem[1]) || perm_visited.include?(mappings[elem[1]])
  40. # new_elem = (Set.new(mappings.keys) - visited).first
  41. # perms << current_perm
  42. # current_perm = []
  43. # perm_visited = Set.new
  44. # else
  45. # new_elem = [elem[1], mappings[elem[1]]]
  46. # end
  47. #
  48. # visited << new_elem[0]
  49. # perm_visited << new_elem[0]
  50. # elem = new_elem
  51. # end
  52. #
  53. # perms << current_perm
  54. #
  55. # pp perms
  56. a = capture
  57. .group_by { |x| x[0] }
  58. .sort
  59. .map { |_, packets| [packets.first[0], packets.first[1]] }
  60. .sort { |x,y| x[1] <=> y[1] }
  61. a = a
  62. .each_with_index.map do |x, i|
  63. [x[1],x[0],if a[i-1][1] == x[1]-1
  64. (x[0]-a[i-1][0])
  65. else
  66. nil
  67. end]
  68. end
  69. .to_a
  70. # # .group_by { |x| x[1] }
  71. # # .sort
  72. # # .to_a
  73. pp a
  74. # a = capture
  75. # .group_by { |x| x[0] }
  76. # .map { |_, packets| packets.first }
  77. # .sort #{ |x, y| x[1] <=> y[1] }
  78. # .map { |x| x }
  79. # .to_a
  80. #
  81. end
  82. # on1 = parse_capture("#{CAPTURES_DIR}/rgbwcct_group1_on.txt")
  83. # on2 = parse_capture("#{CAPTURES_DIR}/rgbwcct_group2_on.txt")
  84. #
  85. # on1_g = on1.group_by { |x| x[0] }
  86. # on2_g = on2.group_by { |x| x[0] }
  87. #
  88. # a = (on1 + on2)
  89. # .map { |x| x[0] }
  90. # .sort
  91. # .uniq
  92. # .map do |key|
  93. # {key: key, vals: {on1: (on1_g[key]||[]).map { |x| x[1] }, on2: (on2_g[key]||[]).map { |x| x[1] } }}
  94. # end
  95. # .to_a
  96. # pp a