Exemplo n.º 1
0
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, " ")
}