You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
973 B
Go
47 lines
973 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"math"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
file, err := ioutil.ReadFile("input.txt")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
inputStrs := strings.Split(strings.Trim(string(file), "\n"), ",")
|
|
|
|
var positions []int
|
|
for _, v := range inputStrs {
|
|
num, _ := strconv.Atoi(v)
|
|
positions = append(positions, num)
|
|
}
|
|
|
|
sort.Ints(positions)
|
|
med := (len(positions)) / 2
|
|
|
|
fuelPartOne, fuelPartTwo := 0, math.MaxInt
|
|
for i := 0; i < len(positions); i++ {
|
|
currentFuel := 0
|
|
for j := 0; j < len(positions); j++ {
|
|
if i == med {
|
|
horizontalDistance := int(math.Abs(float64(positions[j]) - float64(positions[i])))
|
|
fuelPartOne += horizontalDistance
|
|
}
|
|
horizontalDistance := int(math.Abs(float64(i) - float64(positions[j])))
|
|
currentFuel += (horizontalDistance + 1) * horizontalDistance / 2
|
|
}
|
|
if currentFuel < fuelPartTwo {
|
|
fuelPartTwo = currentFuel
|
|
}
|
|
}
|
|
|
|
fmt.Println(fuelPartOne, fuelPartTwo)
|
|
}
|