示例#1
0
func main() {
	starttime := time.Now()

	data := euler.Import("../problemdata/sets.txt")
	sets := make([][]int, len(data))

	for i, line := range data {
		sets[i] = make([]int, 0)
		for _, word := range strings.Split(line, ",") {
			number, _ := strconv.Atoi(word)
			sets[i] = append(sets[i], number)
		}
	}

	total := 0

	for _, set := range sets {
		if isSpecial(set) {
			total += sum(set)
		}
	}

	fmt.Println(total)

	fmt.Println("Elapsed time:", time.Since(starttime))
}
示例#2
0
func main() {

	starttime := time.Now()

	origin := point{0, 0}
	data := euler.Import("../problemdata/triangles.txt")
	total := 0

	for _, line := range data {

		split := strings.Split(line, ",")
		numbers := make([]float64, 6)
		for i, number := range split {
			temp, _ := strconv.Atoi(number)
			numbers[i] = float64(temp)
		}

		point1 := point{numbers[0], numbers[1]}
		point2 := point{numbers[2], numbers[3]}
		point3 := point{numbers[4], numbers[5]}

		f1 := makeLine(point1, point2)
		f2 := makeLine(point2, point3)
		f3 := makeLine(point3, point1)

		inside := onLeft(origin, f1) == onLeft(point3, f1)
		inside = inside && onLeft(origin, f2) == onLeft(point1, f2)
		inside = inside && onLeft(origin, f3) == onLeft(point2, f3)

		if inside {
			total++
		}
	}

	fmt.Println(total)

	fmt.Println("Elapsed time:", time.Since(starttime))
}
示例#3
0
func main() {
	starttime := time.Now()

	size := 80
	data := euler.Import("../problemdata/matrix.txt")
	matrix := make([][]int, len(data))

	for j, line := range data {
		words := strings.Split(line, ",")
		row := make([]int, len(words))
		for i, word := range words {
			row[i], _ = strconv.Atoi(word)
		}
		matrix[j] = row
	}

	dist := make([][]int, size)
	for i := 0; i < size; i++ {
		dist[i] = make([]int, size)
	}

	dist[0][0] = matrix[0][0]

	for i := 1; i < size; i++ {
		dist[0][i] = matrix[0][i] + dist[0][i-1]
	}

	for j := 1; j < size; j++ {
		for i := 0; i < size; i++ {
			dist[j][i] = matrix[j][i] + dist[j-1][i]
		}

	}

	currentSum := 1
	lastSum := 0

	for currentSum != lastSum {

		for i := 0; i < size; i++ {
			for j := 0; j < size; j++ {

				if i != 0 {
					if matrix[i][j]+dist[i-1][j] < dist[i][j] {
						dist[i][j] = matrix[i][j] + dist[i-1][j]
					}
				}

				if j != 0 {
					if matrix[i][j]+dist[i][j-1] < dist[i][j] {
						dist[i][j] = matrix[i][j] + dist[i][j-1]
					}
				}

				if i != size-1 {
					if matrix[i][j]+dist[i+1][j] < dist[i][j] {
						dist[i][j] = matrix[i][j] + dist[i+1][j]
					}
				}

				if j != size-1 {
					if matrix[i][j]+dist[i][j+1] < dist[i][j] {
						dist[i][j] = matrix[i][j] + dist[i][j+1]
					}
				}

			}
		}

		lastSum = currentSum
		currentSum = 0
		for j := 1; j < size; j++ {
			for i := 0; i < size; i++ {
				currentSum += dist[i][j]
			}

		}

	}

	fmt.Println(dist[size-1][size-1])

	fmt.Println("Elapsed time:", time.Since(starttime))
}
示例#4
0
func main() {
	starttime := time.Now()

	data := euler.Import("../problemdata/network.txt")
	n := len(data)
	test := make([][]int, n)

	for i, line := range data {
		test[i] = make([]int, n)
		words := strings.Split(line, ",")
		for j, word := range words {
			number, _ := strconv.Atoi(word)
			test[i][j] = number
		}
	}

	max := 9999999
	min := 0

	unoptimized := 0
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			unoptimized += test[i][j]
		}
	}

	for i := 0; i < n*n; i++ {

		min = 0
		var testi, testj int

		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if test[i][j] < max && test[i][j] > min {
					min = test[i][j]
					testi, testj = i, j
				}
			}
		}

		test[testi][testj] = 0
		test[testj][testi] = 0

		if !isConnected(test) {
			max = min
			test[testi][testj] = min
			test[testj][testi] = min

		}

	}

	total := 0
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			total += test[i][j]
		}
	}

	fmt.Println(unoptimized - total)

	fmt.Println("Elapsed time:", time.Since(starttime))
}