pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Groovy pastebin - collaborative debugging tool View Help


Posted by vollmond on Sun 20 Jul 18:45
report spam | download | new post

  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.                         System.exit(1)
  14.                 }
  15.  
  16.                 def i = new 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. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me