Bit late to the party!
Here is my solution in swift. Incase anyone would like followup code can be found here in Github
func fuel(forMass mass: Int) -> Int { return Int((Double(mass) / 3).rounded(.down) - 2) } func partOne() { var total = 0 input.components(separatedBy: .newlines).map{ input in total = total + fuel(forMass: Int(input) ?? 0) } print(total) } func fuel(forMass mass: Int, includesFuelMass: Bool) -> Int { if !includesFuelMass { return fuel(forMass: mass) } var currentFuel = mass var total = 0 while true { let fuels = fuel(forMass: currentFuel) if fuels < 0 { break } currentFuel = fuels total = total + currentFuel } return total } func partTwo() { var total = 0 input.components(separatedBy: .newlines).map{ input in total = total + fuel(forMass: Int(input) ?? 0, includesFuelMass: true) } print(total) } partOne() partTwo()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Bit late to the party!
Here is my solution in swift. Incase anyone would like followup code can be found here in Github