Submission #2152774

Source Code Expand

Copy
require 'pp'
N = gets.to_i
CARDS = readlines.map{|l| l.split.map{|x| x.to_i}}

#def hashn(cols)
#  ret = Float::INFINITY
#  i = 0
#  while i < 4 
#    a,b,c,d = cols.rotate(i)
#    h = a*1000000000+b*1000000+c*1000+d
#    ret = h if h < ret
#    i += 1
#  end
#  ret
#end
def hashn(cols)
  a,b,c,d = cols
  [
    a*1000000000+b*1000000+c*1000+d,
    b*1000000000+c*1000000+d*1000+a,
    c*1000000000+d*1000000+a*1000+b,
    d*1000000000+a*1000000+b*1000+c,
  ].min
end

COUNTS = Hash.new{|h, k| h[k] = 0}
CARDS.each.with_index do |cols, k|
  COUNTS[hashn(cols)] += 1
end

      #    0--1\
      #    |\3--2
      #    4 | 7|
      #     \5--6
SIDES = [
  [0,3,5,4],
  [3,2,6,5],
  [2,1,7,6],
  [1,0,4,7],
]

ans = 0
# Loop for all the combinations of top/bottom cards
for i in 0..(N-2)
  #p i; break if i == 1
  top = CARDS[i]
  top_hash = hashn(top)
  for j in (i+1)..(N-1)
    # Loop for each rotation of the bottom card
    4.times do |l|
      btm = CARDS[j].rotate(l)
      btm_hash = hashn(btm)
      cols = top + btm

      # The count is, basically, the sum of the number of candidate cards for each side.
      total = 1
      used = Hash.new(0)
      used[top_hash] += 1
      used[btm_hash] += 1
      4.times do |i|
        side_cols = cols.values_at(*SIDES[i])
        side_hash = hashn(side_cols)
        a,b,c,d = side_cols
        m = if a==b&&b==c&&c==d
              4
            elsif a==c&&b==d
              2
            else
              1
            end
        total *= (COUNTS[side_hash] - used[side_hash]) * m
        used[side_hash] += 1
      end
#      p ij: [i, j], top: top, btm: btm,
#        sides: 4.times.map{|i| cols.values_at(*SIDES[i])},
#        total: total if total > 0
      ans += total
    end
  end
end

# Each cube is counted three times, so divide by 3
p ans / 3

Submission Info

Submission Time
Task E - AtCoDeerくんと立方体づくり / Building Cubes with AtCoDeer
User yhara
Language Ruby (2.3.3)
Score 900
Code Size 1897 Byte
Status
Exec Time 3864 ms
Memory 90652 KB

Compile Error

./Main.rb:60: warning: shadowing outer local variable - i

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 0_000.txt, 0_001.txt, 0_002.txt
All 900 / 900 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt
Case Name Status Exec Time Memory
0_000.txt 13 ms 2172 KB
0_001.txt 12 ms 2172 KB
0_002.txt 11 ms 2172 KB
1_003.txt 2431 ms 2684 KB
1_004.txt 189 ms 2684 KB
1_005.txt 2685 ms 2684 KB
1_006.txt 1972 ms 2684 KB
1_007.txt 2747 ms 2684 KB
1_008.txt 2464 ms 2684 KB
1_009.txt 2886 ms 2684 KB
1_010.txt 414 ms 2684 KB
1_011.txt 2918 ms 2812 KB
1_012.txt 28 ms 2812 KB
1_013.txt 3756 ms 78460 KB
1_014.txt 47 ms 3580 KB
1_015.txt 3864 ms 90620 KB
1_016.txt 1008 ms 27388 KB
1_017.txt 3856 ms 90652 KB
1_018.txt 3801 ms 90492 KB
1_019.txt 3826 ms 89468 KB