Compare commits

...

2 Commits

Author SHA1 Message Date
3a6a09ee5a
07b 2021-12-07 07:26:59 +01:00
c35ad03092
07a 2021-12-07 07:10:31 +01:00
5 changed files with 68 additions and 1 deletions

View File

@ -15,7 +15,8 @@ let package = Package(
.executable(name: "03", targets: ["03"]), .executable(name: "03", targets: ["03"]),
.executable(name: "04", targets: ["04"]), .executable(name: "04", targets: ["04"]),
.executable(name: "05", targets: ["05"]), .executable(name: "05", targets: ["05"]),
.executable(name: "06", targets: ["06"]) .executable(name: "06", targets: ["06"]),
.executable(name: "07", targets: ["07"])
], ],
dependencies: [ dependencies: [
// Dependencies declare other packages that this package depends on. // Dependencies declare other packages that this package depends on.
@ -63,6 +64,10 @@ let package = Package(
.executableTarget( .executableTarget(
name: "06", name: "06",
dependencies: [.targetItem(name: "Runner", condition: nil)] dependencies: [.targetItem(name: "Runner", condition: nil)]
),
.executableTarget(
name: "07",
dependencies: [.targetItem(name: "Runner", condition: nil)]
) )
] ]
) )

48
Sources/07/07.swift Normal file
View File

@ -0,0 +1,48 @@
//
// File.swift
//
//
// Created by Max Nuding on 05.12.21.
//
import Foundation
import Runner
struct Day07: Runnable {
let inputPath: String
func run() {
let input = try! String(contentsOfFile: inputPath)
let horizontalPositions = input
.trimmingCharacters(in: .newlines)
.components(separatedBy: ",")
.map { Int($0)! }
runA(horizontalPositions: horizontalPositions)
runB(horizontalPositions: horizontalPositions)
}
func runA(horizontalPositions: [Int]) {
let median = horizontalPositions.sorted()[horizontalPositions.count / 2]
let distancesToMedian = horizontalPositions.map { abs(median - $0) }
let toalFuel = distancesToMedian.reduce(0,+)
print(toalFuel)
}
func runB(horizontalPositions: [Int]) {
let average = Double(horizontalPositions.reduce(0, +)) / Double(horizontalPositions.count)
let roundedDown = Int(floor(average))
let roundedUp = Int(ceil(average))
let fuelForRoundedDown = fuelConsumptionFor(horizontalPositions: horizontalPositions, toPosition: roundedDown)
let fuelForRoundedUp = fuelConsumptionFor(horizontalPositions: horizontalPositions, toPosition: roundedUp)
print(min(fuelForRoundedDown, fuelForRoundedUp))
}
func fuelConsumptionFor(horizontalPositions: [Int], toPosition targetPos: Int) -> Int {
horizontalPositions.map { fuelConsumptionFor(numSteps: abs(targetPos - $0)) }.reduce(0,+)
}
func fuelConsumptionFor(numSteps: Int) -> Int {
let dv = Double(numSteps)
return Int((pow(dv, 2) + dv) / 2.0)
}
}

12
Sources/07/main.swift Normal file
View File

@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Max Nuding on 05.12.21.
//
import Foundation
import Runner
Runner(target: Day07.self, day: "07", isTest: true).run()
Runner(target: Day07.self, day: "07", isTest: false).run()

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,602,216,979,1851,549,9,38,931,28,9,303,933,1262,265,81,291,1447,737,379,456,1236,604,393,438,260,242,421,549,390,372,366,717,927,94,146,603,557,676,1729,4,1027,26,671,213,286,948,142,97,1087,210,72,949,947,1251,414,968,1300,42,814,1036,610,214,1536,210,108,545,375,1248,647,233,406,1502,433,243,13,836,815,73,525,129,323,912,671,270,635,501,110,128,113,147,534,894,883,318,205,453,264,259,1039,759,439,918,1647,493,1,422,503,583,1591,254,267,1213,604,145,242,795,278,181,12,236,1122,1002,235,965,73,228,755,562,91,4,205,1669,25,297,744,36,861,19,875,878,118,147,397,171,199,1286,72,942,200,991,8,496,631,214,668,79,1582,426,240,146,1153,88,1582,204,1254,527,19,358,444,944,84,1573,466,532,118,285,293,126,43,1348,837,326,154,412,153,861,1378,138,730,484,157,42,17,43,553,468,1668,16,301,612,65,293,383,95,260,1219,13,29,322,1043,1303,43,1147,1505,816,836,43,339,825,703,371,289,290,1060,492,661,667,818,178,114,1042,244,383,127,280,500,291,141,2,806,911,252,282,38,348,155,84,138,563,11,304,1216,1312,560,266,328,1570,1330,136,292,25,414,75,1172,387,291,279,12,405,169,661,102,24,66,1149,83,161,120,310,1041,686,149,328,345,1143,190,111,155,183,1210,619,40,431,9,470,731,453,1195,415,641,393,81,234,157,42,911,1365,292,163,388,204,1160,971,13,980,535,285,1504,454,461,518,242,974,798,93,80,720,234,378,1355,351,160,228,407,494,1650,629,621,965,686,341,273,252,66,189,526,15,273,389,1033,126,89,731,1254,1109,83,1660,682,700,712,140,105,929,62,623,476,918,15,568,406,346,720,402,1421,468,702,1090,159,794,14,59,863,393,540,1367,785,824,1016,857,488,515,539,109,12,53,371,13,483,930,112,666,30,664,768,699,1247,449,6,132,61,688,356,46,42,375,1320,606,62,14,233,161,497,150,1084,517,7,128,379,412,264,826,962,101,909,9,1624,77,307,485,106,103,4,1239,173,711,1533,99,762,100,382,876,26,305,218,27,41,1126,11,477,60,1187,201,541,102,571,227,60,118,1561,269,388,233,828,382,195,186,199,1552,444,605,22,78,159,386,1076,813,160,355,860,675,1139,437,71,1605,1,450,163,125,1245,778,10,375,348,18,174,223,367,342,697,257,317,454,242,99,1243,876,719,641,1097,538,444,280,33,99,0,492,194,458,983,42,651,454,1171,621,198,1152,31,410,3,247,977,338,220,830,580,1021,1238,401,536,124,1605,1461,1785,644,837,492,473,1073,1166,78,565,1018,155,156,948,942,368,1140,893,1059,501,46,383,752,373,236,1138,1279,450,104,502,229,783,241,186,46,919,514,371,18,578,428,1205,317,945,71,741,125,1155,680,1081,436,525,1396,171,942,225,403,564,365,372,167,599,955,1442,1672,258,717,922,219,1044,216,224,417,149,510,332,236,533,28,391,39,857,842,601,1216,151,538,900,481,39,363,400,78,1006,384,957,626,1503,269,429,259,755,1140,537,45,1,243,194,815,346,376,304,1167,423,822,166,282,20,200,133,892,140,1073,639,409,1644,105,828,881,1155,41,1590,647,449,89,845,554,411,260,837,237,377,154,49,36,687,62,230,14,11,26,853,497,48,130,17,1817,131,433,106,414,1034,146,977,565,670,581,999,4,164,706,58,212,554,101,64,123,219,154,28,531,626,64,782,505,364,281,1084,1414,27,226,736,507,837,142,434,319,409,819,48,23,820,454,1080,567,783,201,53,1153,404,187,814,28,898,309,380,295,437,366,1226,861,100,550,119,8,233,1452,30,753,381,365,683,474,227,155,1795,1093,208,159,121,1547,942,194,139,325,838,901,597,805,486,1499,597,405,1042,41,222,740,478,884,165,62,547,15,20,309,454,132,5,136,972,45,65,267,463,581,244,51,981,1120,75,410,1310,991,607,1801,950,913,98,769,81,322,19,459,1210,617,48,1163,1243,332,92,1713,120,269,20,246,323,1365,80,1165,1344,236,750,269,62,375,143,163,1444,172,789,521,105,369,1,40,341,625,674,1073,1376,108,391,277,122,181,134,78,1058,103,300,30,848,708,59,93,204,263,32,951,1884,23,488,258,1156,838,72,310,29,209,908,39,119,119,352,70,139,3,884,86,57,541,1553,5,105,710,29,335,1197,1152,144,696,685,948,563

View File

@ -0,0 +1 @@
16,1,2,0,4,2,7,1,2,14