Skip to content

Instantly share code, notes, and snippets.

@mattsan
Last active July 1, 2017 08:49
Show Gist options
  • Save mattsan/3ae74beca67ee844d8ae25e15ccb0a1a to your computer and use it in GitHub Desktop.
Save mattsan/3ae74beca67ee844d8ae25e15ccb0a1a to your computer and use it in GitHub Desktop.
リアルタイムオフラインどう書くE15「ギャスケット」をRubyで解いた
# see http://nabetani.sakura.ne.jp/hena/orde15nohil/
def gasket
g = [[0, 1], [2, nil]]
c = 3
level = 1
13.times do
c = 3 ** level
g = g.zip(g.map {|is| is.map {|i| i + c if i } }).map(&:flatten) +
(g.map {|is| is.map {|i| i + (c * 2) if i } }.map {|is| is + ([nil] * 2 ** level) })
level += 1
end
g
end
GASKET = gasket
FGASKET = GASKET.flatten
SIZE = Math.sqrt(FGASKET.size).to_i
def solve(input)
i = input.to_i
index = FGASKET.index(i)
row = index / SIZE
col = index % SIZE
i1 = (col > 0) ? GASKET[row][col - 1] : nil
i2 = (row > 0) ? GASKET[row - 1][col] : nil
i3 = GASKET[row][col + 1]
i4 = GASKET[row + 1][col]
[i1, i2, i3, i4].compact.sort.join(',')
end
def test(n, input, expected)
actual = solve(input)
print "#{format('%2d', n)}: "
if actual == expected
puts "\x1b[32mpassed\x1b[0m"
else
puts "\x1b[31mfailed input: #{input}, expected: #{expected}, actual: #{actual}\x1b[0m"
end
end
DATA.read.split("\n").each do |line|
n, input, expected = line.split
test(n, input, expected)
end
__END__
0 21 19,22,23
1 0 1,2
2 1 0,3
3 2 0,6
4 3 1,4,5
5 4 3,9
6 9 4,10,11
7 15 11,16,17
8 27 13,28,29
9 32 30
10 47 45,51
11 65 63,69
12 80 78,162
13 199 198,201
14 204 200,205,206
15 243 121,244,245
16 493 492
17 508 507
18 728 726,1458
19 793 792,795
20 902 900,906
21 981 976,982,983
22 1093 1092,2187
23 1202 1200
24 1300 1299,1305
25 1962 1952,1963,1964
26 2188 2187,2190
27 2405 2403,2409
28 3326 3324
29 6561 3280,6562,6563
30 6612 6608,6613,6614
31 7058 7056,7062
32 8444 8442,8448
33 9841 9840,19683
34 15243 15239,15244,15245
35 19946 19944,19950
36 21148 21147
37 39365 39363
38 39366 19682,39367,39368
39 55694 55692,55698
40 57245 57243
41 66430 66429,66432
42 92740 92739
43 115250 115248
44 163031 163029
45 221143 221142,221157
46 410353 410352
47 412649 412647,412659
48 550391 550389
49 699921 699880,699922,699923
50 797161 797160,1594323
51 1000000 999999,1000002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment