Advertisement
Xeeynamo

gnaft

Jun 1st, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.88 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4.  
  5. class Queue<T> {
  6.     var queue : [T]!
  7.     let MaxLen : Int
  8.     var elementsCount : Int
  9.     var curIndex : Int
  10.     var lastIndex : Int
  11.    
  12.     init(length : Int, value : T) {
  13.         queue = [T]()
  14.         MaxLen = length
  15.         elementsCount = 0
  16.         curIndex = 0
  17.         lastIndex = 0
  18.        
  19.         queue.reserveCapacity(length)
  20.         for _ in 1...length {
  21.             queue.append(value)
  22.         }
  23.     }
  24.     func Count() -> Int {
  25.         return elementsCount
  26.     }
  27.     func IsEmpty() -> Bool {
  28.         return Count() == 0
  29.     }
  30.     func Enqueue(value : T) {
  31.         if elementsCount < MaxLen {
  32.             queue[lastIndex % MaxLen] = value
  33.             lastIndex += 1
  34.             elementsCount += 1
  35.         }
  36.     }
  37.     func Dequeue() -> T? {
  38.         if (!IsEmpty()) {
  39.             let i = curIndex
  40.             elementsCount -= 1
  41.             curIndex = (curIndex + 1) % MaxLen
  42.             return queue![i]
  43.         }
  44.         return nil
  45.     }
  46. }
  47. class M {
  48.     let clones = 4
  49.     let count = 10
  50.     let total : Int
  51.     var p1 : Queue<Int>
  52.     var p2 : Queue<Int>
  53.    
  54.     init() {
  55.         total = clones * count
  56.         p1 = Queue<Int>(length: total, value: 0)
  57.         p2 = Queue<Int>(length: total, value: 0)
  58.         Fill()
  59.     }
  60.     private func Run() -> Int {
  61.         var r = 0
  62.         repeat {
  63.             Step();
  64.             r += 1
  65.         } while !p1.IsEmpty() && !p2.IsEmpty()
  66.         return r
  67.     }
  68.     private func Step() {
  69.         let c1 = p1.Dequeue()!
  70.         let c2 = p2.Dequeue()!
  71.         if (c1 > c2) {
  72.             p1.Enqueue(c1)
  73.             p1.Enqueue(c2)
  74.         }
  75.         else if (c1 < c2) {
  76.             p2.Enqueue(c2)
  77.             p2.Enqueue(c1)
  78.         }
  79.         else {
  80.             print("Pair")
  81.             if (p1.Count() < p2.Count()) {
  82.                 p1.Enqueue(c1)
  83.                 p1.Enqueue(c2)
  84.             }
  85.             else if (p1.Count() > p2.Count()) {
  86.                 p2.Enqueue(c2)
  87.                 p2.Enqueue(c1)
  88.             }
  89.             else {
  90.                 print("Pair count")
  91.                 p1.Enqueue(c1)
  92.                 p2.Enqueue(c2)
  93.             }
  94.         }
  95.     }
  96.     private func Fill() {
  97.         var a = [Int]()
  98.         a.reserveCapacity(count)
  99.         for _ in 0..<count {
  100.             a.append(clones)
  101.         }
  102.         FillP(p1, a: &a)
  103.         FillP(p2, a: &a)
  104.     }
  105.     private func FillP(p : Queue<Int>, inout a : [Int]) {
  106.         for _ in 0..<total/2 {
  107.             repeat {
  108.                 let index = Rand()
  109.                 if a[index] > 0 {
  110.                     a[index] -= 1
  111.                     p.Enqueue(index)
  112.                     break
  113.                 }
  114.             } while true
  115.         }
  116.     }
  117.     private func Rand() -> Int {
  118.         return Int(arc4random_uniform(UInt32(count)))
  119.     }
  120. }
  121.  
  122. print(M().Run())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement