123 lines
2.8 KiB
Go
123 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
numbers, err := readInput("input")
|
|
if err != nil {
|
|
fmt.Printf("Error encountered: %s", err)
|
|
return
|
|
}
|
|
|
|
// Alright, I am going for the simplest and most (reasonably) computationally expensive solution
|
|
// So like... Don't hate on me for this XD
|
|
findMinMax := func(slice []int) (min int, max int) {
|
|
min = slice[0]
|
|
max = slice[0]
|
|
for _, value := range slice {
|
|
if value < min {
|
|
min = value
|
|
}
|
|
if value > max {
|
|
max = value
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
min, max := findMinMax(numbers)
|
|
leastFuel := math.MaxInt
|
|
position := 0
|
|
for i := min; i < max; i++ {
|
|
fuel, _ := calculateFuel(numbers, i)
|
|
fmt.Printf("For position %d fuel costs %d\n", i, fuel)
|
|
if fuel < leastFuel {
|
|
leastFuel = fuel
|
|
position = i
|
|
}
|
|
}
|
|
|
|
// Calculating mean
|
|
total := 0.0
|
|
for i := 0; i < len(numbers); i++ {
|
|
total += float64(numbers[i])
|
|
}
|
|
mean := math.Round(total / float64(len(numbers)))
|
|
fmt.Printf("The average is: %f\n", mean)
|
|
|
|
// Calculating median
|
|
sort.Ints(numbers)
|
|
middle := len(numbers) / 2
|
|
median := 0.0
|
|
if middle%2 != 0 {
|
|
median = float64(numbers[middle])
|
|
} else {
|
|
median = float64((numbers[middle-1] + numbers[middle]) / 2)
|
|
}
|
|
fmt.Printf("The median is: %f\n", median)
|
|
fmt.Printf("The least amount of fuel is: %d at position %d\n", leastFuel, position)
|
|
}
|
|
|
|
func calculateFuel(numbers []int, target int) (int, error) {
|
|
fuel := 0
|
|
for i := 0; i < len(numbers); i++ {
|
|
couldBeNegative := (numbers[i] - target)
|
|
if couldBeNegative < 0 {
|
|
couldBeNegative = couldBeNegative * -1
|
|
}
|
|
fuel += couldBeNegative
|
|
}
|
|
return fuel, nil
|
|
}
|
|
|
|
func readInput(inputfile string) ([]int, error) {
|
|
file, err := os.OpenFile(inputfile, 0, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
splitfunc := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
nextIndex := bytes.IndexByte(data, ',')
|
|
if nextIndex > 0 {
|
|
// Returning the next position aka taking only the stuff up to nextIndex and slicing off everything else
|
|
buffer := data[:nextIndex]
|
|
// nextIndex plus 1 because index is at the comma, we want the next number
|
|
return nextIndex + 1, bytes.TrimSpace(buffer), nil
|
|
}
|
|
|
|
// If we are at the end of the buffer, then return the entire buffer, but only if there is data
|
|
if atEOF {
|
|
if len(data) > 0 {
|
|
return len(data), bytes.TrimSpace(data), nil
|
|
}
|
|
}
|
|
|
|
// https://github.com/kgrz/reading-files-in-go/blob/master/comma-separated-string.go
|
|
// For more info
|
|
return 0, nil, nil
|
|
}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
scanner.Split(splitfunc)
|
|
|
|
numbers := make([]int, 0)
|
|
for scanner.Scan() {
|
|
number, err := strconv.Atoi(scanner.Text())
|
|
if err != nil {
|
|
fmt.Printf("Error converting string to integer\n")
|
|
return nil, err
|
|
}
|
|
numbers = append(numbers, number)
|
|
}
|
|
return numbers, nil
|
|
}
|