aoc2021/Sources/07/07.swift

30 lines
742 B
Swift
Raw Normal View History

2021-12-07 06:10:31 +00:00
//
// 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)
}
}