マスクしても同じ数 Yokohama.rb #103

問題の概要

入力は、正の整数。16進数表記になっている。2の28乗未満。
この整数を mask と呼ぶ。
そして。
( mask & n ) == n
であるような正の整数 n を「マスク適合」と呼ぶ。
で。
マスク適合な整数を最大15個出力せよ。順序等は後述。

入力

入力はこんな感じ:
1a
16進数の正の整数。2の28乗未満なので、7桁以下。

出力

出力は、マスク適合である整数の10進数表示を小さい順にコンマ区切りで並べたもの。
例えばこんな
2,8,10,16,18,24,26
具合。
それと。
出力が長すぎると鬱陶しいので、マスク適合な整数が15個を超える場合は、小さい方から13個と大きい方から2個以外は省略して、その部分は代わりに
,...,
を出力すること。
例えばこんな
4,8,12,16,20,24,28,32,36,40,44,48,52,...,2744,2748
具合。

具体例

入力が 1a である場合を例に挙げる。
ちなみに、1a(16進数) は、2進数で 11010 である。

n (10進数) n (2進数) n & mask (2進数) ( mask & n ) == n
1 1 0 false
2 10 10 true
3 11 10 false
4 100 0 false
5 101 0 false
6 110 10 false
7 111 10 false
8 1000 1000 true
9 1001 1000 false
10 1010 1010 true
11 1011 1010 false
12 1100 1000 false
13 1101 1000 false
14 1110 1010 false
15 1111 1010 false
16 10000 10000 true
17 10001 10000 false
18 10010 10010 true
19 10011 10010 false
20 10100 10000 false
21 10101 10000 false
22 10110 10010 false
23 10111 10010 false
24 11000 11000 true
25 11001 11000 false
26 11010 11010 true

補足

サンプルデータ

# 入力 期待
0 1a 2,8,10,16,18,24,26
1 abc 4,8,12,16,20,24,28,32,36,40,44,48,52,...,2744,2748
2 1f7 1,2,3,4,5,6,7,16,17,18,19,20,21,...,502,503
3 1 1
4 a 2,8,10
5 8000000 134217728
6 4001 1,16384,16385
7 8001 1,32768,32769
8 e 2,4,6,8,10,12,14
9 10001 1,65536,65537
10 20001 1,131072,131073
11 40001 1,262144,262145
12 80001 1,524288,524289
13 100001 1,1048576,1048577
14 200001 1,2097152,2097153
15 400001 1,4194304,4194305
16 800001 1,8388608,8388609
17 1000001 1,16777216,16777217
18 2000001 1,33554432,33554433
19 4000001 1,67108864,67108865
20 8000001 1,134217728,134217729
21 c000000 67108864,134217728,201326592
22 f 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
23 ff 1,2,3,4,5,6,7,8,9,10,11,12,13,...,254,255
24 100401 1,1024,1025,1048576,1048577,1049600,1049601
25 400801 1,2048,2049,4194304,4194305,4196352,4196353
26 f0f0f 1,2,3,4,5,6,7,8,9,10,11,12,13,...,986894,986895
27 1001001 1,4096,4097,16777216,16777217,16781312,16781313
28 4002001 1,8192,8193,67108864,67108865,67117056,67117057
29 33333 1,2,3,16,17,18,19,32,33,34,35,48,49,...,209714,209715
30 55555 1,4,5,16,17,20,21,64,65,68,69,80,81,...,349524,349525
31 5415c 4,8,12,16,20,24,28,64,68,72,76,80,84,...,344408,344412
32 fffffff 1,2,3,4,5,6,7,8,9,10,11,12,13,...,268435454,268435455
33 aaaaa 2,8,10,32,34,40,42,128,130,136,138,160,162,...,699048,699050
34 4440a2c 4,8,12,32,36,40,44,512,516,520,524,544,548,...,71567912,71567916
35 9249249 1,8,9,64,65,72,73,512,513,520,521,576,577,...,153391688,153391689
36 e000000 33554432,67108864,100663296,134217728,167772160,201326592,234881024
37 804a88 8,128,136,512,520,640,648,2048,2056,2176,2184,2560,2568,...,8407680,8407688
38 10608 8,512,520,1024,1032,1536,1544,65536,65544,66048,66056,66560,66568,67072,67080
39 1111111 1,16,17,256,257,272,273,4096,4097,4112,4113,4352,4353,...,17895696,17895697
40 68f002a 2,8,10,32,34,40,42,65536,65538,65544,65546,65568,65570,...,110034984,110034986
41 2108421 1,32,33,1024,1025,1056,1057,32768,32769,32800,32801,33792,33793,...,34636832,34636833
42 200e084 4,128,132,8192,8196,8320,8324,16384,16388,16512,16516,24576,24580,...,33611904,33611908
43 1041041 1,64,65,4096,4097,4160,4161,262144,262145,262208,262209,266240,266241,...,17043520,17043521
44 a418021 1,32,33,32768,32769,32800,32801,65536,65537,65568,65569,98304,98305,...,172064800,172064801
45 204081 1,128,129,16384,16385,16512,16513,2097152,2097153,2097280,2097281,2113536,2113537,2113664,2113665
46 1010101 1,256,257,65536,65537,65792,65793,16777216,16777217,16777472,16777473,16842752,16842753,16843008,16843009
47 8000842 2,64,66,2048,2050,2112,2114,134217728,134217730,134217792,134217794,134219776,134219778,134219840,134219842
48 2980140 64,256,320,524288,524352,524544,524608,1048576,1048640,1048832,1048896,1572864,1572928,...,43516160,43516224
49 f010808 8,2048,2056,65536,65544,67584,67592,16777216,16777224,16779264,16779272,16842752,16842760,...,251725824,251725832
50 8040201 1,512,513,262144,262145,262656,262657,134217728,134217729,134218240,134218241,134479872,134479873,134480384,134480385
51 c000003 1,2,3,67108864,67108865,67108866,67108867,134217728,134217729,134217730,134217731,201326592,201326593,201326594,201326595
52 e000003 1,2,3,33554432,33554433,33554434,33554435,67108864,67108865,67108866,67108867,100663296,100663297,...,234881026,234881027
53 f800000 8388608,16777216,25165824,33554432,41943040,50331648,58720256,67108864,75497472,83886080,92274688,100663296,109051904,...,251658240,260046848
54 f000000 16777216,33554432,50331648,67108864,83886080,100663296,117440512,134217728,150994944,167772160,184549376,201326592,218103808,234881024,251658240

JSON 形式のテストデータ

data.json

Ruby - Minitest による雛形

require "minitest/autorun"
require "json"

def solve(src)
  # write something here.
end

if ! ARGV[0] || ! File.exist?( ARGV[0] )
  raise "you should specify json file as ARGV[0]"
end

class TestYokohamaRb103 < Minitest::Test
  json_string = File.open( ARGV[0], &:read )
  data = JSON.parse( json_string, symbolize_names:true )
  data[:test_data].each do | number:, src:, expected: |
    define_method( :"test_#{number}" ) do
      actual = solve(src)
      assert_equal( expected, actual )
    end
  end
end