Completed 7-2

This commit is contained in:
DutchEllie 2021-12-08 12:02:53 +01:00
parent 467940b90e
commit 3e285e26cb
3 changed files with 108 additions and 0 deletions

3
7-2/go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.home.dutchellie.nl/DutchEllie/adventofcode2021/7-2
go 1.17

1
7-2/input Normal file
View File

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,533,1,1251,2,37,207,428,173,711,1167,722,462,640,375,441,952,274,1746,54,233,1381,302,441,1639,298,133,763,51,834,859,1116,93,146,496,464,902,773,1390,162,183,590,1792,79,346,752,1053,711,898,187,118,1105,604,97,1298,172,271,229,353,0,496,91,317,467,136,1459,19,343,189,1197,865,808,555,847,682,51,124,486,191,449,435,890,537,373,577,77,7,59,755,445,1461,413,10,924,918,164,11,1005,854,597,939,131,820,24,1402,376,671,1663,21,117,23,345,280,667,1719,143,1498,751,52,923,573,1252,943,786,20,852,151,9,63,826,916,123,138,59,954,586,1123,629,898,1475,753,1122,47,108,151,217,580,950,72,12,589,196,347,198,10,449,1424,152,713,435,423,446,180,73,259,559,19,291,1826,633,129,17,203,13,230,606,411,494,75,1194,677,658,751,1681,1441,1451,1348,461,327,1755,932,75,1045,48,651,799,1412,19,76,126,957,887,251,146,351,1129,305,36,654,144,735,149,60,310,440,36,154,243,163,522,187,1688,944,374,565,527,282,114,179,41,328,617,432,97,39,71,112,618,1746,8,244,415,490,352,727,608,1463,979,102,436,256,841,1216,118,1408,37,67,529,505,20,12,18,1014,107,823,788,1933,102,16,110,14,300,197,89,592,321,1596,139,66,1614,79,73,256,566,160,684,1321,207,925,415,650,68,921,377,64,1052,96,1301,841,174,480,16,726,348,241,307,1188,1057,463,479,698,613,33,808,178,37,635,115,1724,30,17,80,19,247,719,328,56,24,917,1,422,804,176,32,9,107,15,104,114,193,684,172,667,337,607,883,31,41,358,415,468,761,944,660,134,201,158,78,86,45,117,254,136,267,678,1069,36,224,54,76,904,324,765,9,38,89,109,783,593,10,1313,1345,146,399,344,743,77,74,218,1122,667,226,449,205,174,959,54,376,52,83,156,563,239,178,1097,69,129,417,684,137,85,1246,1347,111,473,1451,18,1294,964,129,648,125,578,338,885,577,546,1201,595,71,323,109,874,73,1855,487,43,1095,1473,636,88,287,282,61,767,171,584,532,269,212,321,1957,1153,339,250,188,484,9,4,176,78,770,2,1020,1105,437,687,239,4,1275,37,1506,623,1044,53,0,47,59,360,730,969,465,37,94,296,956,823,207,24,305,264,126,241,1443,150,624,70,33,1481,84,138,481,365,249,406,140,191,673,600,441,3,6,40,824,237,559,740,1146,4,767,309,1425,559,257,382,758,560,333,1371,9,315,537,28,1256,100,202,586,271,100,1038,6,633,25,1261,148,976,52,261,972,20,150,518,43,21,101,1538,122,132,340,323,393,1183,177,681,9,796,106,364,464,58,856,271,120,225,23,21,135,220,5,46,144,324,1139,1236,383,245,367,304,698,1379,392,1288,220,596,5,416,905,1845,287,4,74,369,1042,643,6,0,852,489,43,592,844,10,12,579,17,243,12,317,21,571,2,1433,427,1438,11,75,226,1293,14,104,1576,213,24,805,127,347,195,564,102,816,696,1278,205,172,238,46,74,205,537,313,1790,142,411,4,1197,208,701,67,216,384,49,362,559,933,626,333,343,241,86,316,891,13,181,850,238,61,93,407,740,660,323,43,190,606,281,660,573,241,386,59,262,1811,1,810,875,71,318,60,368,589,219,1028,934,561,1351,429,635,1382,1693,848,229,82,330,1570,232,132,433,1110,869,904,75,265,358,1132,1536,526,1177,387,1078,96,709,1013,961,600,464,1214,1135,33,696,660,41,599,43,209,807,782,491,62,1552,536,119,406,2,998,191,2,1617,28,116,127,269,425,305,37,1160,396,47,289,1110,510,278,1089,52,145,1016,1413,118,1022,141,185,25,30,254,202,315,53,603,1362,1154,36,20,24,646,177,48,128,178,1179,1621,503,703,744,517,977,13,414,626,472,916,1164,59,340,25,1517,339,896,644,108,615,34,531,84,23,79,13,41,479,108,625,225,893,170,1399,788,164,255,831,5,105,1167,1145,39,219,273,1817,147,299,1023,34,986,189,191,1239,327,595,20,195,199,195,319,93,170,1172,158,945,10,134,42,1337,148,931,893,735,1659,106,1042,1338,881,567,197,569,79,177,625,15,844,1116,484,546,307,1384,205,462,708,595,264,1055,25,186,96,484,326,635,953,1593,966,3,179,69,1210,327,90,122,55,577,214,606,685,463,331,102,823,18,409,440,68,1484,83,80,138,77,24,727,375,1203,81

104
7-2/main.go Normal file
View File

@ -0,0 +1,104 @@
package main
import (
"bufio"
"bytes"
"fmt"
"math"
"os"
"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 := min
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
}
}
fmt.Printf("The least amount of fuel is: %d at position %d\n", leastFuel, position)
}
func calculateFuel(numbers []int, target int) (int, error) {
totalFuel := 0
// For position target, calculate fuel costs
for i := 0; i < len(numbers); i++ {
fuelCosts := 1
for j := 0; j < int(math.Abs(float64(numbers[i]-target))); j++ {
totalFuel += fuelCosts
fuelCosts++
}
}
return totalFuel, 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
}