示例#1
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/horse_dual_%d.txt", 3),
		cgreader.GetFileList("../../output/horse_dual_%d.txt", 3),
		true,
		func(input <-chan string, output chan string) {
			var n int
			fmt.Sscanf(<-input, "%d", &n)

			horses := make([]int, n)
			for i := range horses {
				fmt.Sscanf(<-input, "%d", &horses[i])
			}

			sort.Sort(intArray(horses))

			D := math.MaxInt32
			for i := 1; i < n; i++ {
				x := horses[i-1] - horses[i]

				if x < 0 {
					x *= -1
				}

				if x < D {
					D = x
				}
			}

			output <- fmt.Sprintf("%d", D)
		})
}
示例#2
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/tan_network_%d.txt", 6),
		cgreader.GetFileList("../../output/tan_network_%d.txt", 6),
		true,
		func(input <-chan string, output chan string) {
			// this block could be ommited when solo-running
			minCost = math.MaxUint16
			routes, finalRoute = nil, nil

			start, stop := GetInput(input), GetInput(input)
			hashMap = make(map[uint32]string)
			identifierMap = make(map[string]uint32)
			stationsMC = make(map[uint32]uint16)

			var ns, nr uint32
			fmt.Sscanf(<-input, "%d", &ns)
			stations := make(map[uint32]Station)
			for i := uint32(0); i < ns; i++ {
				station := GetInput(input)
				info := strings.Split(station, ",")
				hashMap[i] = info[0]
				identifierMap[info[0]] = i
				stations[i] = Station{
					info[1][1 : len(info[1])-1],
					ToFloat(info[3]),
					ToFloat(info[4])}
			}

			startHash, finalHash = identifierMap[start], identifierMap[stop]

			if startHash == finalHash {
				output <- stations[startHash].name
				return
			}

			fmt.Sscanf(<-input, "%d", &nr)
			routes = make(map[uint32][]Destination)
			for i := uint32(0); i < nr; i++ {
				route := GetInput(input)
				ra, ro := string(route[:4]), string(route[14:])
				ha, ho := identifierMap[ra], identifierMap[ro]

				a, b := stations[ha], stations[ho]
				cost := GetCost(a.latitude, b.latitude, a.longitude, b.longitude)

				routes[ha] = append(routes[ha], Destination{ho, cost})
			}

			TravelRecursive(0, append(make([]uint32, 0), startHash))

			if finalRoute == nil {
				output <- "IMPOSSIBLE\n"
			} else {
				for _, hash := range finalRoute {
					output <- fmt.Sprintln(stations[hash].name)
				}
			}
		})
}
示例#3
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/telephone_number_%d.txt", 5),
		cgreader.GetFileList("../../output/telephone_number_%d.txt", 5),
		true,
		func(input <-chan string, output chan string) {
			var n int
			fmt.Sscanf(<-input, "%d", &n)

			counter, database := 0, CreateNode()
			for i := 0; i < n; i++ {
				var tel string
				fmt.Sscanf(<-input, "%s", &tel)

				it := database
				for u := range tel {
					var number int
					fmt.Sscanf(string(tel[u]), "%d", &number)

					if _, ok := it.nodes[number]; !ok {
						it.nodes[number] = CreateNode()
						counter++
					}

					it = it.nodes[number]
				}
			}

			output <- fmt.Sprintf("%d", counter)
		})
}
示例#4
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/conway_sequence_%d.txt", 6),
		cgreader.GetFileList("../../output/conway_sequence_%d.txt", 6),
		true,
		func(input <-chan string, output chan string) {
			series, n := make([][]int, 2), 1
			series[0], series[1] = make([]int, 2500), make([]int, 2500)
			var count int
			fmt.Sscanf(<-input, "%d", &series[0][0])
			fmt.Sscanf(<-input, "%d", &count)
			count--

			for i := 0; i < count; i++ {
				pid, cid := i%2, (i+1)%2
				k, c := series[pid][0], 1
				n = 0
				for u := 1; u < len(series[pid]); u++ {
					if v := series[pid][u]; v != k {
						series[cid][n], series[cid][n+1] = c, k
						n += 2
						k, c = v, 1
					} else {
						c++
					}
				}
			}

			id, str := count%2, ""
			for i := 0; i < n; i++ {
				str += fmt.Sprintf("%d ", series[id][i])
			}
			output <- string(str[:len(str)-1])
		})
}
示例#5
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/defibrillator_%d.txt", 4),
		cgreader.GetFileList("../../output/defibrillator_%d.txt", 4),
		true,
		func(input <-chan string, output chan string) {
			var longitude, latitude float64
			var n int

			longitude, latitude = ParseFloat(<-input), ParseFloat(<-input)
			fmt.Sscanf(<-input, "%d", &n)

			var name string

			for distance, i := math.MaxFloat64, 0; i < n; i++ {
				info := strings.Split(<-input, ";")

				lo, la := ParseFloat(info[4]), ParseFloat(info[5])

				x := (lo - longitude) * math.Cos(latitude+la)
				y := la - latitude
				d := x*x + y*y

				if d < distance {
					distance, name = d, info[1]
				}
			}

			output <- fmt.Sprintf("%s", name)
		})
}
示例#6
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/cgx_formatter_%d.txt", 12),
		cgreader.GetFileList("../../output/cgx_formatter_%d.txt", 12),
		true,
		func(input <-chan string, output chan string) {
			// program logic
		})
}
示例#7
0
func GetEmbedFiles(jsonInfo *simplejson.Json, files []string) []string {
	if i, err := jsonInfo.String(); err != nil {
		if files, err = jsonInfo.StringArray(); err != nil {
			if jsonPath := jsonInfo.Get(INFO_PATH); jsonPath != nil {
				if path, errPath := jsonPath.String(); errPath == nil {
					if jsonAmount := jsonInfo.Get(INFO_AMOUNT); jsonAmount != nil {
						if amount, errAmount := jsonAmount.Int(); errAmount == nil {
							if strings.Contains(path, "*") {
								path = strings.Replace(path, "*", "%d", 1)
								files = cgreader.GetFileList(path, amount)
							} else {
								ErrorIllegalEmbbedFormatSmartPath()
							}
						} else {
							ErrorMessage(errAmount.Error())
						}
					} else {
						ErrorMissingInEmbedFormat(INFO_AMOUNT)
					}
				} else {
					ErrorMessage(errPath.Error())
				}
			} else {
				ErrorMissingInEmbedFormat(INFO_PATH)
			}
		}
	} else {
		files = append(files, i)
	}

	return files
}
示例#8
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/chuck_norris_%d.txt", 4),
		cgreader.GetFileList("../../output/chuck_norris_%d.txt", 4),
		true,
		func(input <-chan string, output chan string) {
			input_message, message := <-input, ""

			for i := range input_message {
				character := int(input_message[i])
				if character != '\n' {
					var msg string
					for u := 0; u < 7; u++ {
						if character%2 == 0 {
							msg = "0" + msg
						} else {
							msg = "1" + msg
						}
						character /= 2
					}
					message += msg
				}
			}

			is_zero, counter, lc := message[0] == '0', 0, len(message)-1

			for i := range message {
				if iz, il := message[i] == '0', i == lc; il || is_zero != iz {
					if il && iz == is_zero {
						counter++
					}

					output <- PrintCNGroup(is_zero, counter)

					if il && iz != is_zero {
						output <- " " + PrintCNGroup(iz, 1)
					} else if !il {
						output <- " "
					}

					is_zero, counter = !is_zero, 0
				}
				counter++
			}
		})
}
示例#9
0
func main() {
	cgreader.RunInteractivePrograms(
		"kirk",
		cgreader.GetFileList("../../input/kirk_%d.txt", 6),
		false,
		Initialize,
		Update)
}
示例#10
0
func main() {
	cgreader.RunInteractivePrograms(
		"ragnarok",
		cgreader.GetFileList("../../input/ragnarok_%d.txt", 4),
		true,
		Initialize,
		Update)
}
示例#11
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/scrabble_%d.txt", 7),
		cgreader.GetFileList("../../output/scrabble_%d.txt", 7),
		true,
		func(input <-chan string, output chan string) {
			var n int
			fmt.Sscanf(<-input, "%d", &n)

			dictionary := make([]string, n)
			for i := range dictionary {
				fmt.Sscanf(<-input, "%s", &dictionary[i])
			}

			var w string
			fmt.Sscanf(<-input, "%s", &w)

			word := GetWordInformation(w)

			bw, bwp := "", uint(0)
			for i := range dictionary {
				info, points := GetCharacterAmount(dictionary[i]), uint(0)
				for c, n := range info {
					if value, ok := word[c]; ok {
						if n > value.amount {
							n = value.amount
						}

						points += value.points * n
					} else {
						points = 0
						break
					}
				}
				if points > bwp {
					bwp, bw = points, dictionary[i]
				}
			}

			output <- bw
		})
}
示例#12
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/snakes_and_ladders_%d.txt", 5),
		cgreader.GetFileList("../../output/snakes_and_ladders_%d.txt", 5),
		true,
		func(input <-chan string, output chan string) {
			var n int
			fmt.Sscanf(<-input, "%d", &n)

			Squares = make([]SALSquare, n)
			var start int
			for i := range Squares {
				var t string
				fmt.Sscanf(<-input, "%s", &t)
				switch t {
				case "S", "R":
					Squares[i] = RSquare{}
					if t == "S" {
						start = i
					}
				case "E":
					Squares[i] = ESquare{}
				default:
					var x int
					fmt.Sscanf(t, "%d", &x)
					Squares[i] = MSquare{x}
				}
			}

			max := n
			LAOS = max
			Squares[start].Move([]int{}, start, n, 0)

			if LAOS == max {
				output <- fmt.Sprintf("impossible")
			} else {
				output <- fmt.Sprintf("%d", LAOS)
			}
		})
}
示例#13
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/dwarfs_giants_%d.txt", 3),
		cgreader.GetFileList("../../output/dwarfs_giants_%d.txt", 3),
		true,
		func(input <-chan string, output chan string) {
			max, relations = 0, make(map[int][]int)

			var n int
			fmt.Sscanf(<-input, "%d", &n)

			for i := 0; i < n; i++ {
				var r, c int
				fmt.Sscanf(<-input, "%d %d", &r, &c)
				relations[r] = append(relations[r], c)
			}

			for _, children := range relations {
				TravelRecursive(children, 1)
			}

			output <- fmt.Sprintf("%d", max)
		})
}
示例#14
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/network_cabling_%d.txt", 8),
		cgreader.GetFileList("../../output/network_cabling_%d.txt", 8),
		true,
		func(input <-chan string, output chan string) {
			var n int
			fmt.Sscanf(<-input, "%d", &n)

			if n <= 1 {
				output <- "0"
				return
			}

			yPositions := make(map[int]int)
			averageYPositions := make([]int, n)
			minX, maxX, aY, aN, m := math.MaxInt32, 0, 0, 0, 0
			for i := 0; i < n; i++ {
				var x, y int
				fmt.Sscanf(<-input, "%d %d", &x, &y)

				if x < minX {
					minX = x
				} else if x > maxX {
					maxX = x
				}

				yPositions[y]++
				if yPositions[y] > m {
					aY, m, aN = y, yPositions[y], 1
					averageYPositions[0] = y
				} else if yPositions[y] == m {
					aY += y
					averageYPositions[aN] = y
					aN++
				}
			}

			aY /= aN
			cY, distance := aY, math.MaxInt32
			for i := 0; i < aN; i++ {
				y := averageYPositions[i]
				d := y - aY

				if d < 0 {
					d *= -1
				}

				if d < distance {
					cY, distance = y, d
				}
			}

			distance = maxX - minX
			for k, v := range yPositions {
				d := cY - k
				if d < 0 {
					d *= -1
				}
				distance += d * v
			}

			output <- fmt.Sprintf("%d", distance)
		})
}
示例#15
0
func main() {
	cgreader.RunStaticPrograms(
		cgreader.GetFileList("../../input/surface_%d.txt", 9),
		cgreader.GetFileList("../../output/surface_%d.txt", 9),
		true,
		func(input <-chan string, output chan string) {
			var G, h, v, g, o, i, t uint32
			var L, H, N, X, Y int
			var LINE string
			var V rune
			var w waterInfo

			fmt.Sscanf(<-input, "%d", &L)
			fmt.Sscanf(<-input, "%d", &H)

			WORLD, GROUPS := make([]uint32, L*H), make(map[uint32][]waterInfo)

			for Y = 0; Y < H; Y++ {
				LINE = <-input
				for X, V = range LINE {
					h, v = uint32(Y*L), 0
					i = h + uint32(X)
					if V == WATER {
						if t = i - 1; X != 0 && WORLD[t]&WATER_MASK == 1 {
							v = WORLD[t]
						}

						if t = i - uint32(L); h != 0 && WORLD[t]&WATER_MASK == 1 {
							if v&WATER_MASK == 1 {
								g, o = v>>GROUP_SHIFT, WORLD[t]>>GROUP_SHIFT

								if g != o {
									for _, w = range GROUPS[o] {
										WORLD[w.position] = v
									}

									GROUPS[g] = append(GROUPS[g], GROUPS[o]...)
									delete(GROUPS, o)
								}
							} else {
								v = WORLD[t]
								g = v >> GROUP_SHIFT
							}

							GROUPS[g] = append(GROUPS[g], waterInfo{v, i})
						} else if v&WATER_MASK == 1 {
							g = v >> GROUP_SHIFT
							GROUPS[g] = append(GROUPS[g], waterInfo{v, i})
						} else {
							g = G
							G++

							v = (g << GROUP_SHIFT) | 1

							GROUPS[g] = append(GROUPS[g], waterInfo{v, i})
						}

						WORLD[i] = v
					}
				}
			}

			fmt.Sscanf(<-input, "%d", &N)

			for ; N > 0; N-- {
				fmt.Sscanf(<-input, "%d %d", &X, &Y)
				t = uint32(Y*L + X)

				if WORLD[t]&WATER_MASK == 0 {
					output <- "0\n"
				} else {
					g = WORLD[t] >> GROUP_SHIFT
					output <- fmt.Sprintf("%d\n", len(GROUPS[g]))
				}
			}
		})
}