adventofcode2021/7-1/main.go

123 lines
2.8 KiB
Go
Raw Normal View History

2021-12-08 11:06:29 +01:00
package main
2021-12-08 11:12:34 +01:00
import (
"bufio"
"bytes"
"fmt"
2021-12-08 11:26:52 +01:00
"math"
2021-12-08 11:12:34 +01:00
"os"
2021-12-08 11:46:03 +01:00
"sort"
2021-12-08 11:12:34 +01:00
"strconv"
)
2021-12-08 11:06:29 +01:00
func main() {
2021-12-08 11:12:34 +01:00
numbers, err := readInput("input")
if err != nil {
fmt.Printf("Error encountered: %s", err)
return
}
2021-12-08 11:26:52 +01:00
// 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)
2021-12-08 11:46:03 +01:00
leastFuel := math.MaxInt
2021-12-08 11:26:52 +01:00
position := 0
for i := min; i < max; i++ {
fuel, _ := calculateFuel(numbers, i)
2021-12-08 11:46:03 +01:00
fmt.Printf("For position %d fuel costs %d\n", i, fuel)
2021-12-08 11:26:52 +01:00
if fuel < leastFuel {
leastFuel = fuel
position = i
}
}
2021-12-08 11:46:03 +01:00
// 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)
2021-12-08 11:26:52 +01:00
}
func calculateFuel(numbers []int, target int) (int, error) {
fuel := 0
for i := 0; i < len(numbers); i++ {
2021-12-08 11:46:03 +01:00
couldBeNegative := (numbers[i] - target)
if couldBeNegative < 0 {
couldBeNegative = couldBeNegative * -1
}
fuel += couldBeNegative
2021-12-08 11:26:52 +01:00
}
return fuel, nil
2021-12-08 11:12:34 +01:00
}
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
2021-12-08 11:06:29 +01:00
}