Return to problem list

2019-1A Problem B

Final solution

cmd.go:
save   open on GitHub
// package, import, etc...

func start() {
	var t, maxNights, maxGophers int
	mustReadLineOfInts(&t, &maxNights, &maxGophers)
	for i := 0; i < t; i++ {
		test(maxNights, maxGophers)
	}
}

func test(maxNights, maxGophers int) {
	validGopherAmounts := make([]int, 0, maxGophers)
	for i := 1; i <= maxGophers; i++ {
		validGopherAmounts = append(validGopherAmounts, i)
	}
	queriesRem := maxNights
	mod := 18
	for {
		if queriesRem == 0 {
			os.Exit(0)
		}
		queriesRem--
		modStr := strconv.Itoa(mod)
		for i := 0; i < 18; i++ {
			stdout.WriteString(modStr)
			if i < 17 {
				stdout.WriteByte(' ')
			}
		}
		stdout.WriteByte('\n')
		stdout.Flush()
		windmails := mustReadLineOfIntsIntoArray()
		if len(windmails) == 1 {
			os.Exit(0)
		}
		assert(len(windmails) == 18)
		sum := 0
		for _, w := range windmails {
			sum += w
		}
		newValidGopherAmounts := make([]int, 0, len(validGopherAmounts))
		for _, gophers := range validGopherAmounts {
			if gophers%mod == sum%mod {
				newValidGopherAmounts = append(newValidGopherAmounts, gophers)
			}
		}
		validGopherAmounts = newValidGopherAmounts
		if len(validGopherAmounts) == 1 {
			stdout.WriteString(strconv.Itoa(validGopherAmounts[0]))
			stdout.WriteByte('\n')
			stdout.Flush()
			var verdict int
			mustReadLineOfInts(&verdict)
			if verdict == -1 {
				os.Exit(0)
			}
			return
		} else if len(validGopherAmounts) == 0 {
			os.Exit(0)
		}
		if mod == 2 {
			mod = 18
		} else {
			mod--
		}
	}
}

// boilerplate omitted...