コード例 #1
0
ファイル: TrainTimetable.go プロジェクト: gcapell/go-codejam
func solver(in *ProblemReader.ProblemReader) string {
	turnAround := in.Num()
	nums := in.NNums(2)
	nA, nB := nums[0], nums[1]
	allTrips := make([]trip, nA+nB)

	for _, v := range allStations {
		v.clear()
	}

	for j := 0; j < nA; j++ {
		allTrips[j] = readTrip(in, A, B)
	}
	for j := 0; j < nB; j++ {
		allTrips[nA+j] = readTrip(in, B, A)
	}
	sort.Sort(tripArray(allTrips))

	for _, t := range allTrips {
		src, dst := allStations[t.src], allStations[t.dst]

		// fmt.Printf("trip:%v, from %v to %v\n", t, src, dst)

		src.getTrain(t.depart)
		dst.addTrain(t.arrive + turnAround)
	}

	return fmt.Sprintf("%d %d", allStations[A].reserved, allStations[B].reserved)
}
コード例 #2
0
ファイル: StoreCredit.go プロジェクト: gcapell/go-codejam
func solver(in *ProblemReader.ProblemReader) string {
	credit := in.Num()
	items := in.Num()
	prices := in.NNums(items)

	p0, p1 := pairSum(credit, prices)
	return fmt.Sprintf("%d %d", p0+1, p1+1)
}
コード例 #3
0
func solver(in *ProblemReader.ProblemReader) string {
	n := in.Num()
	v1 := in.NNums(n)
	v2 := in.NNums(n)

	sort.SortInts(v1)
	sort.SortInts(v2)

	var sum int64 = 0

	for j := 0; j < n; j++ {
		sum += int64(v1[j]) * int64(v2[n-j-1])
	}
	return fmt.Sprint(sum)
}
コード例 #4
0
ファイル: RopeIntranet.go プロジェクト: gcapell/go-codejam
func solver(in *ProblemReader.ProblemReader) string {
	n := in.Num()
	wires := make([]Wire, n)

	for j := 0; j < n; j++ {
		wire := in.NNums(2)
		wires[j] = Wire{wire[0], wire[1]}
	}
	sort.Sort(WireSlice(wires))

	cross := 0
	for j := 0; j < n; j++ {
		for k := j + 1; k < n; k++ {
			if wires[k].b < wires[j].b {
				cross++
			}
		}
	}
	return fmt.Sprintf("%d", cross)
}
コード例 #5
0
ファイル: Milkshakes.go プロジェクト: gcapell/go-codejam
func solver(in *ProblemReader.ProblemReader) string {
	flavours := in.Num()
	nCustomers := in.Num()

	customers := make([]*customer, nCustomers)
	for j := 0; j < nCustomers; j++ {
		customers[j] = newCustomer(in.Nums())
	}

	malted := make(map[int]bool, flavours)
	for j := 0; j < flavours; j++ {
		malted[j+1] = false
	}

	keepLooking := true
	for keepLooking {
		keepLooking = false
		// Is there anybody unhappy?
		for _, c := range customers {
			if !c.happyWith(malted) {
				// Unhappy, but can be made happy with malt
				if c.likesMalted && !malted[c.malted] {
					malted[c.malted] = true
					keepLooking = true
				} else {
					return "IMPOSSIBLE"
				}
			}
		}
	}

	reply := make([]string, flavours)
	for j := 0; j < flavours; j++ {
		reply[j] = ternary(malted[j+1], "1", "0")
	}
	return strings.Join(reply, " ")
}
コード例 #6
0
func solver(in *ProblemReader.ProblemReader) string {
	nEngines := in.Num()

	engines := make([]string, nEngines)
	for j := 0; j < nEngines; j++ {
		engines[j] = in.Line()
	}

	terms := in.Num()
	available := make(map[string]bool)
	reset(available, engines, "")
	changes := 0
	for j := 0; j < terms; j++ {
		term := in.Line()
		if _, ok := available[term]; ok {
			available[term] = false, false
			if len(available) == 0 {
				changes++
				reset(available, engines, term)
			}
		}
	}
	return fmt.Sprintf("%d", changes)
}