Snippets

cia_rana オフラインリアルタイムどう書く E08 の問題 - 白黒陣取りゲーム

Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
 end
 
 def calc_area friends, enemies
-  friends.each.with_index(1).with_object(Set.new){|((r, c), i), area_unit|
+  friends.each.with_index(1).with_object(Set.new){|((r, c), index), area_unit|
     # 自陣営の全ての駒の対角となりうる駒を集める
     # 自分のy座標より大きいy座標の値を持ち、かつ同じx座標を持つ自陣営の駒を集める
-    next unless y = friends[i..-1].select{|_,d|d == c}.map{|s,_|s}
+    next unless y = friends[index..-1].select{|_,d|d == c}.map{|s,_|s}
     # 自分のx座標より大きいx座標の値を持ち、かつ同じy座標を持つ自陣営の駒を集める
-    next unless x = friends[i..-1].select{|s,_|s == r}.map{|_,d|d}
+    next unless x = friends[index..-1].select{|s,_|s == r}.map{|_,d|d}
     
     # 自陣営の中から上で集めたx,y座標と一致する駒を抜き出す
-    z = get_opposite_angle friends, enemies, i, y, x
+    z = get_opposite_angle friends, enemies, index, y, x
     next if z.empty?
     
     # エリアの面積を計算
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
     next if z.empty?
     
     # エリアの面積を計算
+    # r: top-left, c: top-right, z[0]: bottom-left, z[1]: bottom-right
     (AREA_DIGIT_SIZE*r).step(AREA_DIGIT_SIZE*(z[0]-1), AREA_DIGIT_SIZE) {|i|
       (c...z[1]).each {|j|
         area_unit << i + j
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
 end
 
 def calc_area friends, enemies
-  # 自陣営の全ての駒の対角となりうる駒を集める
-  self_area = []
-  friends.each.with_index(1){|(r, c), i|
+  friends.each.with_index(1).with_object(Set.new){|((r, c), i), area_unit|
+    # 自陣営の全ての駒の対角となりうる駒を集める
     # 自分のy座標より大きいy座標の値を持ち、かつ同じx座標を持つ自陣営の駒を集める
     next unless y = friends[i..-1].select{|_,d|d == c}.map{|s,_|s}
     # 自分のx座標より大きいx座標の値を持ち、かつ同じy座標を持つ自陣営の駒を集める
     z = get_opposite_angle friends, enemies, i, y, x
     next if z.empty?
     
-    self_area << [r, c, *z]
-  }
-  
-  # エリアの面積を計算
-  self_area.each.with_object(Set.new){|(lu, ru, ld, rd), area_unit|
-    (AREA_DIGIT_SIZE*lu).step(AREA_DIGIT_SIZE*(ld-1), AREA_DIGIT_SIZE) {|y|
-      (ru...rd).each {|x|
-        area_unit << y + x
+    # エリアの面積を計算
+    (AREA_DIGIT_SIZE*r).step(AREA_DIGIT_SIZE*(z[0]-1), AREA_DIGIT_SIZE) {|i|
+      (c...z[1]).each {|j|
+        area_unit << i + j
       }
     }
   }.size
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
   }
   
   # エリアの面積を計算
-  area_unit = Set.new
-  self_area.each{|lu, ru, ld, rd|
+  self_area.each.with_object(Set.new){|(lu, ru, ld, rd), area_unit|
     (AREA_DIGIT_SIZE*lu).step(AREA_DIGIT_SIZE*(ld-1), AREA_DIGIT_SIZE) {|y|
       (ru...rd).each {|x|
         area_unit << y + x
       }
     }
-  }
-  area_unit.size
+  }.size
 end
 
 def solve input
Updated by cia_rana

File e08.rb Modified

  • Ignore whitespace
  • Hide word diff
+require "set"
 require "minitest/autorun"
 
 AREA_SIZE = 18
   }
   
   # エリアの面積を計算
-  area_unit = {}
+  area_unit = Set.new
   self_area.each{|lu, ru, ld, rd|
     (AREA_DIGIT_SIZE*lu).step(AREA_DIGIT_SIZE*(ld-1), AREA_DIGIT_SIZE) {|y|
       (ru...rd).each {|x|
-        area_unit[y + x] = true
+        area_unit << y + x
       }
     }
   }
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.