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 abuse | download | new post

  1. //SearchEngines - Google codejam
  2.  
  3. class Timetable {
  4.         static def testCase = 0
  5.         static def numTripsFromA = new ArrayList()
  6.         static def numTripsFromB = new ArrayList()
  7.         static def trips = new ArrayList()
  8.         static def turnaround
  9.         static def curSource
  10.         static def availA
  11.         static def availB
  12.  
  13.         static void main(args) {
  14.                 if (args.length != 1) {
  15.                         println "Usage: <command> <path to input>"
  16.                         System.exit(1)
  17.                 }
  18.  
  19.                 def seenFirstLine = false
  20.                 def timeForNums = false
  21.  
  22.                 new File(args[0]).splitEachLine(" ") { lineFields ->
  23.                         if (lineFields.size() == 1) {
  24.                                 if (seenFirstLine) {
  25.                                         if (testCase > 0) {
  26.                                                 processData()
  27.                                         }
  28.                                        
  29.                                         turnaround = new Integer(lineFields[0])
  30.                                         timeForNums = true
  31.                                         testCase++
  32.                                         trips = new ArrayList()
  33.                                 } else {
  34.                                         seenFirstLine = true
  35.                                 }
  36.                         } else {
  37.                                 if (timeForNums) {
  38.                                         numTripsFromA = new Integer(lineFields[0])
  39.                                         numTripsFromB = new Integer(lineFields[1])
  40.                                         timeForNums = false
  41.                                 } else {
  42.                                         def fieldFields = lineFields[0].tokenize(":")
  43.                                         if (fieldFields.size() > 1) {
  44.                                                 if (numTripsFromA > 0) {
  45.                                                         trips.add(new TripData(depart:lineFields[0], arrive:lineFields[1], source:"A"))
  46.                                                         numTripsFromA--
  47.                                                 } else if (numTripsFromB > 0) {
  48.                                                         trips.add(new TripData(depart:lineFields[0], arrive:lineFields[1], source:"B"))
  49.                                                         numTripsFromB--
  50.                                                 }
  51.                                         }
  52.                                 }
  53.                         }
  54.                 }
  55.  
  56.                 processData()
  57.         }
  58.  
  59.         static processData() {
  60.                 def startA = 0
  61.                 def startB = 0
  62.                 availA = 0
  63.                 availB = 0
  64.                 def curTime
  65.  
  66.                 sortTrips()
  67.  
  68.                 //println "turnaround = ${turnaround} \n $trips"
  69.  
  70.                 trips.each() { trip ->
  71.                         curTime = trip.depart
  72.                         updateTimes(curTime)
  73.                         if (trip.source == "A") {
  74.                                 if (availA > 0) {
  75.                                         availA--
  76.                                 } else {
  77.                                         startA++
  78.                                 }
  79.                         } else if (trip.source == "B") {
  80.                                 if (availB > 0) {
  81.                                         availB--
  82.                                 } else {
  83.                                         startB++
  84.                                 }
  85.                         }
  86.                 }
  87.  
  88.                 println "Case #${testCase}: ${startA} ${startB}"
  89.         }
  90.  
  91.         static updateTimes(curTime) {
  92.                 for (i in 0..trips.size()-1) {
  93.                         if (addTime(trips[i].arrive, turnaround) <= curTime && ! trips[i].done) {
  94.                                 trips[i].done = true
  95.                                 if (trips[i].source == "A") {
  96.                                         availB++
  97.                                 } else {
  98.                                         availA++
  99.                                 }
  100.                         }
  101.                 }
  102.         }
  103.  
  104.         static addTime(time, turnaround) {
  105.                 def timeFields = time.tokenize(":")
  106.                 def hours = new Integer(timeFields[0])
  107.                 def minutes = new Integer(timeFields[1])
  108.                 minutes += turnaround
  109.  
  110.                 if (minutes > 59) {
  111.                         hours++
  112.                         minutes -= 60
  113.                 }
  114.                
  115.                 if ("${minutes}".size() < 2) {
  116.                         minutes = "0${minutes}"
  117.                 }
  118.  
  119.                 if ("${hours}".size() < 2) {
  120.                         hours = "0${hours}"
  121.                 }
  122.  
  123.                 return "${hours}:${minutes}"
  124.         }
  125.  
  126.         static sortTrips() {
  127.                 def tmp
  128.                 def swapped = true
  129.  
  130.                 while (swapped) {
  131.                         swapped = false
  132.                         (trips.size() - 1).times() { i ->
  133.                                 if (trips[i].depart > trips[i+1].depart ||
  134.                                         (trips[i].depart == trips[i+1].depart && trips[i].arrive > trips[i+1].arrive)) {
  135.                                         swapped = true
  136.                                         tmp = trips[i]
  137.                                         trips[i] = trips[i+1]
  138.                                         trips[i+1] = tmp
  139.                                 }
  140.                         }
  141.                 }
  142.         }
  143. }

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 so that I can delete my post