Guest
Public paste!

vollmond

By: a guest | Jul 20th, 2008 | Syntax: Groovy | Size: 1.58 KB | Hits: 112 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/env groovy
  2. class ImprovedTimetable {
  3.         static def testCase = 0
  4.         static def available
  5.         static def availA
  6.         static def availB
  7.         static def trips
  8.         static def turnaround
  9.  
  10.         static void main(args) {
  11.                 if (args.length != 1) {
  12.                         println "Usage: <command> <path to input>"
  13.                         5.0%2Fdocs%2Fapi%2F">System.exit(1)
  14.                 }
  15.  
  16.                 def i = new 5.0%2Fdocs%2Fapi%2F">File(args[0]).readLines().tail().iterator()
  17.                 while (i.hasNext()) {
  18.                         testCase++
  19.                         trips = []
  20.                         turnaround = i.next().toInteger()
  21.                         def numTrips = i.next().split()
  22.                        
  23.                         numTrips[0].toInteger().times() {
  24.                                 def times = i.next().split()
  25.                                 trips.add([depart:convert(times[0]), arrive:convert(times[1]), source:"A"])
  26.                         }
  27.  
  28.                         numTrips[1].toInteger().times() {
  29.                                 def times = i.next().split()
  30.                                 trips.add([depart:convert(times[0]), arrive:convert(times[1]), source:"B"])
  31.                         }
  32.  
  33.                         processData()
  34.                 }
  35.         }
  36.  
  37.         static convert(stringTime) {
  38.                 def time = stringTime.tokenize(":")
  39.                 return (time[0].toInteger() * 60) + time[1].toInteger()
  40.         }
  41.  
  42.         static processData() {
  43.                 def starting = [A:0, B:0]
  44.                 available = [A:0, B:0]
  45.                 def curTime
  46.  
  47.                 trips = trips.sort { it.depart }
  48.                 trips.each() { trip ->
  49.                         curTime = trip.depart
  50.                         updateTimes(curTime)
  51.                         if (available[trip.source] > 0) {
  52.                                 available[trip.source]--
  53.                         } else {
  54.                                 starting[trip.source]++
  55.                         }
  56.                 }
  57.  
  58.                 println "Case #${testCase}: ${starting["A"]} ${starting["B"]}"
  59.         }
  60.  
  61.         static updateTimes(curTime) {
  62.                 trips.size().times { i ->
  63.                         if (trips[i].arrive + turnaround <= curTime && ! trips[i].done) {
  64.                                 trips[i].done = true
  65.                                 available["A" == trips[i].source ? "B" : "A"]++
  66.                         }
  67.                 }
  68.         }
  69. }