This commit is contained in:
2021-12-07 07:10:31 +01:00
parent c14ff89c45
commit c35ad03092
4 changed files with 47 additions and 1 deletions

29
Sources/07/07.swift Normal file
View 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
View 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()