diff --git a/7/input b/7/input new file mode 100644 index 0000000..663ded8 --- /dev/null +++ b/7/input @@ -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,433,308,307,379,19,1597,200,1143,1316,269,366,1461,143,262,231,767,229,156,186,252,650,760,33,148,379,313,376,924,812,251,132,354,481,228,969,773,3,1514,204,0,1193,327,91,73,914,74,33,96,407,323,1637,475,54,1126,250,719,581,9,305,1442,376,362,622,540,595,92,1333,1523,686,466,117,59,827,300,814,203,248,969,565,1276,1280,801,486,10,604,652,62,214,219,242,146,749,312,402,1317,690,120,856,814,100,74,790,308,420,72,186,370,1209,1826,11,230,327,96,1235,1075,994,836,46,233,143,40,826,1566,323,414,443,42,263,43,995,336,141,641,404,222,264,1253,330,406,1710,324,383,817,34,555,53,86,188,463,118,777,283,1,621,44,1347,477,311,557,19,243,1069,81,885,93,33,271,217,1052,789,291,156,655,510,999,1696,770,77,501,291,486,32,671,823,484,1005,622,661,0,290,1176,100,47,141,439,780,871,474,1129,33,494,1135,209,146,68,109,512,271,460,1162,261,551,123,40,1499,345,245,62,1092,580,82,171,75,194,291,333,83,39,136,885,23,290,702,38,1,47,634,15,190,116,856,398,850,463,1211,1078,132,61,775,508,35,158,0,58,849,610,0,350,66,517,36,105,1234,953,195,60,1483,211,448,41,728,590,964,41,684,790,801,745,404,75,281,72,501,578,1697,537,184,967,1633,1115,1458,725,314,1266,979,750,186,243,1716,88,714,733,697,512,130,68,219,239,19,752,72,577,636,283,1137,564,446,374,52,83,195,475,41,764,120,1187,154,828,260,560,168,93,867,257,24,1409,513,1109,463,291,81,897,1116,687,381,4,1859,166,1400,405,32,1247,257,418,495,197,676,2,918,1225,658,1496,633,1430,1124,594,289,8,9,217,695,116,184,122,1122,915,724,163,1055,59,710,802,1669,75,697,407,1,1610,533,240,655,619,581,35,128,620,884,10,1540,50,437,1159,771,140,992,425,733,598,1563,219,240,169,107,687,41,1385,500,86,369,229,204,72,1345,261,472,275,1364,642,259,876,41,139,1,1182,586,888,674,1172,309,247,573,168,418,232,982,731,733,655,272,0,123,70,1067,905,29,185,791,102,131,38,1299,8,217,24,341,149,932,56,117,419,1049,909,207,681,193,957,215,658,295,529,436,674,543,551,492,270,616,1201,253,496,1725,223,43,1107,170,1242,1562,131,507,449,34,274,1100,52,28,204,808,684,359,711,1077,713,623,963,666,566,1576,734,24,824,764,466,1408,737,1200,1520,582,213,455,162,715,1339,115,261,19,544,43,1032,796,596,1452,339,1469,1229,157,737,289,145,153,10,485,781,491,137,1188,194,107,56,4,157,99,868,220,370,493,6,393,715,510,442,39,434,385,719,398,523,285,47,147,1119,37,168,937,282,1308,388,170,421,204,163,85,126,558,61,97,95,668,20,153,48,1008,324,8,694,1349,1445,562,1049,69,191,49,269,110,176,940,173,14,237,1192,657,693,792,224,52,147,758,123,4,190,933,370,805,903,19,597,28,247,487,93,1330,123,1481,731,771,0,406,120,1425,700,158,217,1000,717,1207,1377,443,88,58,476,999,40,424,62,41,13,171,40,832,116,456,670,129,665,659,69,337,8,160,1012,26,186,1451,1101,174,33,798,11,1517,487,179,995,109,765,1325,51,935,831,474,245,89,1436,623,623,1308,95,60,596,1571,672,426,16,1710,592,129,949,242,35,760,195,1017,88,83,537,733,98,332,551,133,1415,667,479,214,596,469,193,342,18,88,1343,1475,230,495,113,206,380,238,819,991,650,1392,221,228,693,724,703,611,642,187,55,327,198,980,240,397,169,55,329,177,498,710,1104,875,972,550,580,119,161,367,771,1737,138,219,1414,42,998,606,190,413,925,1184,1064,826,92,827,368,19,272,604,138,107,351,246,540,527,403,92,70,420,666,309,48,625,1796,712,1634,85,260,1030,17,563,71,728,833,207,655,68,4,394,225,49,18,899,92,607,10,867,0,571,115,692,77,578,687,213,456,85,393,865,42,766,605,872,486,613,1460,93,737,172,52,777,25,684,429,802,591,3,1090,95,601,104,241,576,361,779,648,338,83,12,784,1073,188,667,65,32,16,1079,550,108,694,571,192,372,1504,43,1600,446,227,208,319,569,859,54,1188,51,1067,551,1032,649,753,8,982,169,1477,593,986,877,675,505,607,164,756,447,956,195,419,1094,823,1789,279,684,51,1186,1801,627,1379 diff --git a/7/solve.go b/7/solve.go new file mode 100644 index 0000000..46559e3 --- /dev/null +++ b/7/solve.go @@ -0,0 +1,67 @@ +package main + +import ( + "bufio" + "fmt" + "math" + "os" + "strconv" + "strings" +) + +func main() { + // too lazy for fancy algorithms + var crabs []int + topCrab := 0 + + s := bufio.NewScanner(os.Stdin) + s.Scan() + for _, crabStr := range strings.Split(s.Text(), ",") { + crab, _ := strconv.Atoi(crabStr) + crabs = append(crabs, crab) + if crab > topCrab { + topCrab = crab + } + } + + // part 1 + bestFuel := math.MaxInt64 + + for x := 0; x <= topCrab; x++ { + fuel := 0 + for _, crab := range crabs { + fuel += abs(crab - x) + } + + if fuel < bestFuel { + bestFuel = fuel + } + } + + fmt.Println(bestFuel) + + // part 2 + bestFuel2 := math.MaxInt64 + + for x := 0; x <= topCrab; x++ { + fuel := 0 + for _, crab := range crabs { + diff := abs(crab - x) + // triangular number + fuel += (diff * (diff + 1)) / 2 + } + + if fuel < bestFuel2 { + bestFuel2 = fuel + } + } + + fmt.Println(bestFuel2) +} + +func abs(i int) int { + if i < 0 { + return -i + } + return i +}