// // animalTransport.swift // codesprint 12 // // Created by Avery Jones on 2017-12-14. // Copyright © 2017 Avery Jones. All rights reserved. // import Foundation struct Animal { var type : Int var source : Int var destination : Int var distance : Int init(typeOf : Int, from : Int, to : Int) { type = typeOf source = from destination = to if (destination < source) //MAY BE REDUNDANT { distance = 0 } else { distance = destination - source } } func printSelf(){ print("Animal with type \(type), going from \(source) -> \(destination)") } } struct Schedule { var lineup = [Animal]() var pool = [Animal]() var busyUntil = 0 var currentType = 0 var currentSize = 0 init(_ temp: [Animal]) { lineup = [] pool = temp busyUntil = 0 currentType = 0 currentSize = 0 } func isCompatibleWith(additional : Animal) -> Bool { //print("\nComparing Compatibility") if currentType == 0 { // print("1") return true } if(additional.type == currentType) { // print("1") return true } else { if additional.source >= busyUntil { // print("1") return true } } //print("0") return false } mutating func append(next : Animal) { lineup.append(next) currentSize += 1 if next.destination > busyUntil { busyUntil = next.destination currentType = next.type } } func printSelf() { print("Current Size is \(currentSize)\nLinupElements are:") for a in lineup { print(a.type, a.source, a.destination) } print("\n") } } //MARK: FUNCS func addAnimal(primary : inout Schedule, secondary : inout Schedule) -> Int { var primaryOut = false var secondaryOut = false if iterateN(list: &primary) { if let element = primary.pool.first { primary.append(next: element) primary.pool.removeFirst() primaryOut = true } } if iterateN(list: &secondary) { if let element = secondary.pool.first { secondary.append(next: element) secondary.pool.removeFirst() secondaryOut = false } } if !primaryOut && !secondaryOut { return -1 } if !primaryOut { return 1 } if !secondaryOut { return 2 } return 0 } func iterateN(list : inout Schedule) -> Bool { while list.pool.count > 0 && !list.isCompatibleWith(additional: list.pool[0]) { list.pool.removeFirst() } if list.pool.count == 0 { return false } return true } func chooseFirst(primary : inout Schedule, secondary : inout Schedule) { primary.append(next: primary.pool.removeFirst()) secondChoice(list: &secondary) } func secondChoice(list : inout Schedule) { _ = list.append(next: list.pool.removeFirst()) while list.pool.count > 0 && list.isCompatibleWith(additional: list.pool[0]) { list.pool.removeFirst() } if list.pool.count == 0 { return } _ = list.append(next: list.pool.removeFirst()) list.lineup.removeFirst() list.currentSize = list.lineup.count list.busyUntil = list.lineup.first!.destination } func easiestAnimal(list: Schedule) -> Animal? //MERGE WITH firstNonCompatible to avoid 2N { var ii = 0 while !list.isCompatibleWith(additional: list.pool[ii]) && ii < list.pool.count { ii += 1 } if ii == list.pool.count { return nil } return list.pool[ii] } func firstNonCompatible(list: Schedule) -> Animal? { var ii = 0 while list.isCompatibleWith(additional: list.pool[ii]) { ii += 1 } if ii == list.pool.count { return nil } return list.pool[ii] } //MARK: MAIN let queries = Int(readLine()!)! for _ in 0.. Bool in //Sort by destination if first.destination < second.destination { return true } return false }) var primary = Schedule(animals), secondary = Schedule(animals) chooseFirst(primary: &primary, secondary: &secondary) outerloop : for ii in 1.. secondary.busyUntil { animalDistances.append(String(primary.busyUntil)) continue } else { animalDistances.append(String(secondary.busyUntil)) continue } } for _ in 0..