Beispiel #1
0
func mergeFactors(a, b map[int]int) map[int]int {
	newFactors := map[int]int{}
	for key, _ := range a {
		newFactors[key] = misc.Max(a[key], b[key])
	}

	for key, _ := range b {
		newFactors[key] = misc.Max(a[key], b[key])
	}

	return newFactors
}
Beispiel #2
0
func maxTotal(triStr string) int {
	lines := strings.Split(triStr, "\n")

	triangle := [][]int{}
	for _, line := range lines {
		newRow := []int{}

		elements := strings.Split(line, " ")

		for _, elem := range elements {
			x, err := strconv.Atoi(elem)
			if err == nil {
				newRow = append(newRow, x)
			}
		}

		if len(newRow) > 0 {
			triangle = append(triangle, newRow)
		}
	}

	// Transform the triangle so that any cell will contain the maximum value
	// of a downward path starting at that point
	for i := len(triangle) - 2; i >= 0; i-- {
		for j, _ := range triangle[i] {
			left := triangle[i+1][j]
			right := triangle[i+1][j+1]

			triangle[i][j] += misc.Max(left, right)
		}
	}

	// Return the maximum path starting from the top of the triangle
	return triangle[0][0]
}
Beispiel #3
0
func findMaxProduct(numbers []int, length int) int {
	maxProduct := -1
	for i := 0; i < len(numbers)-length+1; i++ {
		maxProduct = misc.Max(misc.Product(numbers[i:i+length]), maxProduct)
	}

	return maxProduct
}
Beispiel #4
0
func main() {
	windowSize := 13

	maxProduct := 0
	for i := 0; i < len(n)-windowSize+1; i++ {
		maxProduct = misc.Max(digitsProduct(n[i:i+windowSize]), maxProduct)
	}

	fmt.Println("Max product: ", maxProduct)
}
Beispiel #5
0
func main() {
	// Translate string representation into 2D slice representation
	grid := [][]int{}
	str := s

	for i, row := range strings.Split(str, ",") {
		grid = append(grid, []int{})
		for _, digits := range strings.Fields(row) {
			number, _ := strconv.Atoi(digits)
			grid[i] = append(grid[i], number)
		}
	}

	// Grid is already a slice of the rows
	sequences := append(grid, getColumns(grid)...)
	sequences = append(sequences, getDiagonals(grid)...)
	length := 4
	maxProduct := -1
	fmt.Println(sequences)
	for _, seq := range sequences {
		maxProduct = misc.Max(maxProduct, findMaxProduct(seq, length))
	}
	fmt.Println("Largest product: ", maxProduct)
}