Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add solutions to Y2008 Round 1A problems A/B.

  • Loading branch information...
commit 6e44ef0e8ce8b69f3704e792f5468dcbc0c45767 1 parent f09cfe0
Mick Killianey authored
View
38 src/codejam/y2008/round1a/ProblemAMinimumScalarProduct.gsp
@@ -0,0 +1,38 @@
+// Solution for Google code jam 2008 Round 1A Problem A: Minimum Scalar Product
+// http://code.google.com/codejam/contest/32016/dashboard#s=p0
+
+uses codejam.SolutionRunner
+uses java.io.BufferedReader
+uses java.io.StringReader
+uses java.lang.Long
+uses java.util.List
+
+var sampleInput = new StringReader({
+ "2",
+ "3",
+ "1 3 -5",
+ "-2 4 1",
+ "5",
+ "1 2 3 4 5",
+ "1 0 1 0 1",
+ ""}.join("\n"))
+
+function solve(x : List<Long>, y : List<Long>) : String {
+ x.sort()
+ y.sortDescending()
+ var sum : long = 0
+ for (xi in x index i) {
+ sum += xi * y[i]
+ }
+ return "${sum}"
+}
+
+var runner = SolutionRunner.from(\ reader -> {
+ var count = reader.readLine().toInt()
+ var vector1 = reader.readLine().split(" ").map( \ elt -> elt.toLong()).toList()
+ var vector2 = reader.readLine().split(" ").map( \ elt -> elt.toLong()).toList()
+ return solve(vector1, vector2)
+})
+
+runner.solveAll(sampleInput)
+runner.pollDirectory(:prefix = "A")
View
91 src/codejam/y2008/round1a/ProblemBMilkshakes.gsp
@@ -0,0 +1,91 @@
+uses java.lang.AssertionError
+
+// Solution for Google code jam 2008 Round 1A Problem B: Milkshakes
+// http://code.google.com/codejam/contest/32016/dashboard#s=p1
+
+uses codejam.SolutionRunner
+uses java.io.BufferedReader
+uses java.io.StringReader
+uses java.lang.Integer
+uses java.util.ArrayList
+uses java.util.List
+
+var sampleInput = new StringReader({
+ "2",
+ "5",
+ "3",
+ "1 1 1",
+ "2 1 0 2 0",
+ "1 5 0",
+ "1",
+ "2",
+ "1 1 0",
+ "1 1 1",
+ ""}.join("\n"))
+
+class Customer {
+ var _malted : Integer as MaltedFavorite
+ var _unmalted : List<Integer> as UnmaltedFavorites
+ function hasMaltedFavorite() : boolean { return _malted != null }
+ override function toString() : String { return "${MaltedFavorite}: ${UnmaltedFavorites}" }
+}
+
+function solve(totalFlavors : int, customers : ArrayList<Customer>) : String {
+ //print("${totalFlavors} flavors, ${customers.size()} customers:")
+ //for (customer in customers) {
+ // print(" ${customer}")
+ //}
+ var isMalted = (0..|totalFlavors).map(\ _ -> false)
+ while (true) {
+ var allCustomersSatisfied = true
+ for (customer in customers index i) {
+ if (customer.hasMaltedFavorite() and isMalted[customer.MaltedFavorite - 1]) {
+ //print("Customer ${i}: ${customer} likes ${customer.MaltedFavorite}")
+ continue
+ }
+ var unmaltedFavorite = customer.UnmaltedFavorites.firstWhere( \ flavor -> !isMalted[flavor - 1])
+ if (unmaltedFavorite != null) {
+ //print("Customer ${i}: ${customer} likes ${unmaltedFavorite}")
+ continue
+ }
+ if (customer.hasMaltedFavorite()) {
+ print("Try making ${customer.MaltedFavorite} malted for customer ${customer}")
+ isMalted[customer.MaltedFavorite - 1] = true
+ allCustomersSatisfied = false
+ continue
+ }
+ print("Customer ${i}: ${customer} is unsatisfiable")
+ return "IMPOSSIBLE"
+ }
+ if (allCustomersSatisfied) {
+ return isMalted.map( \ elt -> elt ? "1" : "0").join(" ")
+ }
+ }
+ throw new AssertionError("Should not reach this line")
+}
+
+var runner = SolutionRunner.from(\ reader -> {
+ var totalFlavors = reader.readLine().toInt()
+ var numCustomers = reader.readLine().toInt()
+ var customers : ArrayList<Customer> = new ArrayList<Customer>(numCustomers)
+ for (_ in 0..|numCustomers) {
+ var line : Integer[] = reader.readLine().split(" ").map(\ elt -> elt.toInt())
+ var flavorsLiked = line[0]
+ var malted : Integer = null
+ var unmalted : List<Integer> = {}
+ for (i in 0..|flavorsLiked) {
+ var flavor = line[2*i + 1]
+ if (line[2*i + 2] == 0) {
+ unmalted.add(flavor)
+ } else {
+ malted = flavor
+ }
+ }
+ var customer = new Customer() { :MaltedFavorite = malted, :UnmaltedFavorites = unmalted }
+ customers.add(customer)
+ }
+ return solve(totalFlavors, customers)
+})
+
+runner.solveAll(sampleInput)
+runner.pollDirectory(:prefix = "B")
Please sign in to comment.
Something went wrong with that request. Please try again.