Learning new environments can be painful so it’s good it starts off easy.
I’m trying to solve them with Swift Playgrounds on the iPad, for example. And I can’t figure out how to create a simple text file for the data.
I’m trying to solve them with Swift Playgrounds on the iPad, for example. And I can’t figure out how to create a simple text file for the data.
This year, I’m learning Python by using a Raspberry Pi 4 directly connected to my iPad via USB. Should be another fun month!
func calcFuel(_ mass:Int) -> Int {mass / 3 - 2}
let z0 = massList.map(calcFuel).reduce(0, +)
or simply ... let z1 = massList.map({$0 / 3 - 2}).reduce(0, +)
Any idea how to read the data on the iPad?Anyway, a complete Swift Playground on iPad solution:
import UIKit
let path = Bundle.main.url(forResource: "input01", withExtension: "txt")
var text = try String(contentsOf: path!, encoding: .utf8)
let massList:[Int] = text.components(separatedBy: .whitespacesAndNewlines).compactMap({Int($0)})
let z1 = massList.map({$0 / 3 - 2}).reduce(0, +)
print("\(z1)") let path = Bundle.main.url(forResource: "input01", withExtension: "txt")
var text = try String(contentsOf: path!, encoding: .utf8)Besides, there are immediate additional questions:
- what's a bundle, and why does a Bundle deal with urls?
- What's the difference between Bundle.main.url and FileManager.main.urls, and why one and not the other?
- where does the resource come from?
- how do I read a file from a path?
- Why is String responsible for reading (and writing!) contents of a file? In which world is this good design?