07a
This commit is contained in:
parent
c14ff89c45
commit
c35ad03092
@ -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)]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
29
Sources/07/07.swift
Normal file
29
Sources/07/07.swift
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
//
|
||||||
|
// 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)! }
|
||||||
|
run(horizontalPositions: horizontalPositions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(horizontalPositions: [Int]) {
|
||||||
|
let median = horizontalPositions.sorted()[horizontalPositions.count / 2]
|
||||||
|
let distancesToMedian = horizontalPositions.map { abs(median - $0) }
|
||||||
|
let toalFuel = distancesToMedian.reduce(0,+)
|
||||||
|
print(toalFuel)
|
||||||
|
}
|
||||||
|
}
|
11
Sources/07/main.swift
Normal file
11
Sources/07/main.swift
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Max Nuding on 05.12.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import Runner
|
||||||
|
|
||||||
|
Runner(target: Day07.self, day: "07", isTest: false).run()
|
1
Sources/Runner/Resources/input/07_test.txt
Normal file
1
Sources/Runner/Resources/input/07_test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
16,1,2,0,4,2,7,1,2,14
|
Loading…
Reference in New Issue
Block a user